当前位置:嗨网首页>书籍在线阅读

09-find_last_of()系列

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

F.5.4 find_last_of()系列

find_last_of()方法的原型如下:

size_type find_last_of (const basic_string& str,
                        size_type pos = npos) const noexcept;
size_type find_last_of (const charT* s, size_type pos, size_type n) const;
size_type find_last_of (const charT* s, size_type pos = npos) const;
size_type find_last_of (charT c, size_type pos = npos) const noexcept;

这些方法与对应rfind()方法的工作方式相似,但它们不是搜索整个子字符串,而是搜索子字符串中的字符出现的最后位置。

下面的代码在一个字符串中查找字符串“hat”和“any”中字母最后出现的位置:

string longer("That is a funny hat.");
string shorter("hat");
size_type loc1 = longer.find_last_of(shorter); // sets loc1 to 18
size_type loc2 = longer.find_last_of("any");   // sets loc2 to 17

在longer中,最后出现的hat中的字符是hat中的t,而最后出现的any中的字符是hat中的a。