如何在std :: string到达字符串结尾时"重置"它的查找成员函数

Iow*_*a15 -2 c++ string stl std

有没有办法我可以"重置"函数std :: string :: find再次查看字符串的开头类似于在i/o流中设置文件指针?谢谢.

Luc*_*ore 8

你的假设是错误的.find总是查找第一个匹配(或指定起始索引后的第一个匹配)

std::string str("Hello");

size_t x = str.find("l");
assert(x==2);

x = str.find("l");
assert(x==2);
Run Code Online (Sandbox Code Playgroud)

要查找下一场比赛,您必须指定一个开始位置:

x = str.find("l",x+1);  //previous x was 2
assert(x==3);

x = str.find("l",x+1); //now x is 3, no subsequent 'l' found
assert(x==std::string::npos);
Run Code Online (Sandbox Code Playgroud)