V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
tesion99
V2EX  ›  C

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

  •  
  •   tesion99 · 2015-09-11 11:37:17 +08:00 · 1177 次点击
    这是一个创建于 3143 天前的主题,其中的信息可能已经有所发展或是发生改变。

    参考来源: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 才对啊???

    4 条回复    2015-09-11 12:33:42 +08:00
    RecursiveG
        1
    RecursiveG  
       2015-09-11 11:56:01 +08:00
    "*begin* at or before position pos"
    只要开头 index 小于等于 pos 即可
    sakeven
        2
    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
        3
    tesion99  
    OP
       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
        4
    tesion99  
    OP
       2015-09-11 12:33:42 +08:00
    @sakeven 这样解释好像更合理
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5175 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 08:12 · PVG 16:12 · LAX 01:12 · JFK 04:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.