C++ string::rfind 的一点疑惑

2015-09-11 11:37:17 +08:00
 tesion99

参考来源:http://www.cplusplus.com/reference/string/string/rfind/
对于 rfind 中的 pos 参数的说法是 pos 是搜索的最后的位置(即截止位置)
string str ("The sixth sick sheik's sixth sheep's sick.");
string key ("sixth");

str.rfind (key, 3 ) // -1
以上结果还能解释的通,即查找的截止字符串是"The " 这个子串
str.rfind (key, 4 ) // 4
这个结果就不知道如何出来的了, 查找的截止字符串不是"The s", 没有找到 key 才对啊???

1189 次点击
所在节点    C
4 条回复
RecursiveG
2015-09-11 11:56:01 +08:00
"*begin* at or before position pos"
只要开头 index 小于等于 pos 即可
sakeven
2015-09-11 11:59:51 +08:00
应该是指 key 的第一个字符可以出现的最后位置。
Position of the last character in the string to be considered as the beginning of a match.
tesion99
2015-09-11 12:01:22 +08:00
@RecursiveG :这个意思是说搜索 从 0 <= index <=pos;
一下是 find 的说法:
When pos is specified, the search only includes characters at or after position pos, ignoring any possible occurrences that include characters before pos.
find 是从 pos<= index 开始检索的
tesion99
2015-09-11 12:33:42 +08:00
@sakeven 这样解释好像更合理

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/219890

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX