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;
如果我想检查一个字符是否存在于 std::string
中,就性能而言,哪个是首选?
最佳答案
没关系。他们做同样的事情。
从字面上看。 libstdc++ just delegates find_first_of(char, size_t)
to find(char, size_t)
, as does libc++ ,还有 MSVS 2015(感谢 roalz)。任何实现都没有理由不这样做。
我不太清楚为什么 find_first_of
的重载甚至存在;它可能只是为了与 find
对称(当您使用其他重载时它会做一些不同的事情)但老实说这让我感到困惑。
关于c++ - 哪个更好地检查 std::string 中是否存在字符?查找还是 find_first_of?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53724121/