c++ - 为什么 string::find_first_of() 返回意外结果?

标签 c++ string algorithm c++11 while-loop

我的代码如下:

string s_1 = "ssissippi";
string s = "si";
size_t pos = s_1.find_first_of(s);
while (pos != string::npos)
{
    cout << pos << endl;
    pos++;
    pos = s_1.find_first_of(s, pos);
}

我得到这样的结果: 0,1,2,3,4,5,8.
我不知道是什么导致了答案。 如果有任何帮助,我将不胜感激。

最佳答案

您似乎是指成员函数 find() 而不是 find_first_of()

例如:

string s_1 = "ssissippi";
string s = "si";
size_t pos = s_1.find(s);
while (pos != string::npos)
{
    cout << pos << endl;
    pos++;
    pos = s_1.find(s, pos);
}

成员函数 find_first_of() 查找源字符串中第一个位置,其中一个字符存储在指定为参数的字符串中。

关于c++ - 为什么 string::find_first_of() 返回意外结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40723095/

相关文章:

ios - swift 不能用 String 调用 isKindOfClass

algorithm - 选择有约束的排列

c++ - 我如何最好地对 N 值的(一维) vector 进行强制展平?

c++ - BOOST_SPIRIT_DEBUG_NODE 无法在变体节点上编译

c++ - 从 C++(特别是 netstat)获取命令行程序的输出

c++ - 在 C++ 中使用它在内存中的起始地址编辑字符串

c++ - GCC 中的 std::string 实现及其短字符串的内存开销

java - 计算字符串中子字符串的出现次数

algorithm - 正常的 union/find 算法在没有任何额外工作的情况下是线程安全的吗?

c++ - 这个递归函数的时间复杂度是多少