问题

软件 >> C++
Questions in category: C++ (C++).

[C++] find_last_of

Posted by haifeng on 2024-04-08 09:58:20 last update 2024-04-08 10:03:20 | Answers (0) | 收藏


C++ 中的 find_last_of() 函数经常会被误认为在字符串中搜寻某个字串最后出现的位置. 实际上它的功能是在字符串中搜索与其参数中指定的任何字符匹配的最后一个字符.

find_last_of() 有以下多种声明.

string (1)
size_t find_last_of (const string& str, size_t pos = npos) const;
c-string (2)
size_t find_last_of (const char* s, size_t pos = npos) const;
buffer (3)
size_t find_last_of (const char* s, size_t pos, size_t n) const;
character (4)
size_t find_last_of (char c, size_t pos = npos) const;

 

现在输入 symbol(x y what),  希望从末尾开始搜寻`)`, 则建议使用 

size_t rfind (char c, size_t pos = npos) const;

rfind() 会从指定的 pos 参数开始在字符串中往前寻找第一次出现的字符c.

 

参考

cplusplus.com/reference/string/string/find_last_of/