如何"reset" std::string的查找成员函数,一旦它到达字符串的末尾
How to "reset" the find member function of std::string once it reaches end of string
有没有一种方法可以"重置"函数std::string::find,再次查看字符串的开头,类似于在I/o流中设置文件指针?谢谢
您的假设是错误的。find
总是查找第一个匹配(或指定起始索引后的第一个匹配)
std::string str("Hello");
size_t x = str.find("l");
assert(x==2);
x = str.find("l");
assert(x==2);
为了寻找下一场比赛,你必须指定一个起始位置:
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);
实际上find
搜索给定索引后的第一个匹配项。这是默认的原型:
size_t find (const string& str, size_t pos = 0) const noexcept;
默认情况下,它开始查看字符串的索引0,因此:
str.find(str2);
正在搜索str
中首次出现的str2
。如果没有发现任何内容,则返回std::string::npos
你可以使用这样的功能:
str.find(str2, 4);
它将从索引4开始搜索str
中第一个出现的str2
。如果字符串str
的字符少于4个,它将再次返回std::string::npos
。
相关文章:
- 字符串函数在目标C++上运行时C++返回空
- 字符串函数返回奇怪的值
- 这里的字符串函数是什么意思
- 在字符串函数中抛出'char const*'实例后调用的终止
- 为什么 stl 字符串C++函数有时出错有时会出错?
- 从C++为字符串函数设置_ENV
- 来自 2D 数组的字符串函数
- 返回最长的公共子字符串函数 c ++
- 使用getline和字符串函数C++程序中的分段错误
- 在字符串函数的指针中返回地址
- 字符串函数:strcat()
- 这个子字符串函数有什么问题
- 字符串函数不返回字符串? C++
- 使用向量和字符串函数返回字符串
- 尝试修复一个字符串函数,该函数采用字符串并通过替换一些单词来更改它
- 逐字读取字符数组,无需字符串函数
- 我从我的字符串函数返回到主函数
- 为什么我不能从字符串函数返回字符串数组?
- 如何在不使用字符串函数的情况下在char数组中找到字符串?(C )
- 带有 #include< iostream.h> 的程序允许使用 strlen() 等字符串函数。为什么?