哪个更好检查一个字符是否存在于std :: string中?find或find_first_of?

xml*_*lmx 0 c++ string algorithm performance stl

std::string 有两个不同的成员函数做同样的事情:

size_type find( CharT ch, size_type pos = 0 ) const noexcept;
size_type find_first_of( CharT ch, size_type pos = 0 ) const noexcept;
Run Code Online (Sandbox Code Playgroud)

如果我想检查a中是否存在某个字符std::string,哪个字符在性能方面是首选?

Lig*_*ica 5

没关系.他们做同样的事情.

就像字面意思一样.的libstdc ++仅委派find_first_of(char, size_t)find(char, size_t),一样的libc ++和MSVS 2015年太(感谢roalz).没有理由进行任何其他实施.

我不清楚为什么find_first_of甚至存在过载; 它可能只是为了对称find(当你使用其他重载时它做了一些不同的事情),但说实话,这似乎让我感到困惑.