跳转到内容
主菜单
主菜单
移至侧栏
隐藏
导航
首页
最近更改
随机页面
MediaWiki帮助
代码酷
搜索
搜索
中文(中国大陆)
外观
创建账号
登录
个人工具
创建账号
登录
未登录编辑者的页面
了解详情
贡献
讨论
编辑“︁
C++ 字符串搜索
”︁(章节)
页面
讨论
大陆简体
阅读
编辑
编辑源代码
查看历史
工具
工具
移至侧栏
隐藏
操作
阅读
编辑
编辑源代码
查看历史
常规
链入页面
相关更改
特殊页面
页面信息
外观
移至侧栏
隐藏
您的更改会在有权核准的用户核准后向读者展示。
警告:
您没有登录。如果您进行任何编辑,您的IP地址会公开展示。如果您
登录
或
创建账号
,您的编辑会以您的用户名署名,此外还有其他益处。
反垃圾检查。
不要
加入这个!
== 搜索函数详解 == === `find()`函数 === `find()`函数用于在主字符串中查找子字符串或字符的第一次出现。其基本语法如下: <syntaxhighlight lang="cpp"> size_t find(const std::string& str, size_t pos = 0) const; size_t find(const char* s, size_t pos = 0) const; size_t find(char c, size_t pos = 0) const; </syntaxhighlight> 其中: - `str`或`s`是要查找的子字符串。 - `c`是要查找的字符。 - `pos`是开始搜索的位置(默认为0)。 如果找到子字符串或字符,则返回其起始位置(索引);否则返回`std::string::npos`。 ==== 示例 ==== <syntaxhighlight lang="cpp"> #include <iostream> #include <string> int main() { std::string str = "Hello, world! Welcome to C++ programming."; std::string substr = "world"; // 查找子字符串 size_t found = str.find(substr); if (found != std::string::npos) { std::cout << "Substring found at position: " << found << std::endl; } else { std::cout << "Substring not found." << std::endl; } // 查找字符 found = str.find('W'); if (found != std::string::npos) { std::cout << "Character found at position: " << found << std::endl; } else { std::cout << "Character not found." << std::endl; } return 0; } </syntaxhighlight> 输出: <pre> Substring found at position: 7 Character found at position: 14 </pre> === `rfind()`函数 === `rfind()`函数与`find()`类似,但它从字符串的末尾开始搜索,返回子字符串或字符的最后一次出现的位置。 ==== 示例 ==== <syntaxhighlight lang="cpp"> #include <iostream> #include <string> int main() { std::string str = "C++ is fun. C++ is powerful."; std::string substr = "C++"; size_t found = str.rfind(substr); if (found != std::string::npos) { std::cout << "Last occurrence found at position: " << found << std::endl; } else { std::cout << "Substring not found." << std::endl; } return 0; } </syntaxhighlight> 输出: <pre> Last occurrence found at position: 13 </pre> === 其他搜索函数 === - `find_first_of()`:查找字符集合中任意字符的第一次出现。 - `find_last_of()`:查找字符集合中任意字符的最后一次出现。 - `find_first_not_of()`:查找不在字符集合中的第一个字符。 - `find_last_not_of()`:查找不在字符集合中的最后一个字符。 ==== 示例 ==== <syntaxhighlight lang="cpp"> #include <iostream> #include <string> int main() { std::string str = "C++ programming is fun!"; std::string vowels = "aeiouAEIOU"; // 查找第一个元音字母 size_t found = str.find_first_of(vowels); if (found != std::string::npos) { std::cout << "First vowel found at position: " << found << std::endl; } // 查找第一个非字母字符 found = str.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"); if (found != std::string::npos) { std::cout << "First non-alphabetic character found at position: " << found << std::endl; } return 0; } </syntaxhighlight> 输出: <pre> First vowel found at position: 1 First non-alphabetic character found at position: 3 </pre>
摘要:
请注意,所有对代码酷的贡献均被视为依照知识共享署名-非商业性使用-相同方式共享发表(详情请见
代码酷:著作权
)。如果您不希望您的文字作品被随意编辑和分发传播,请不要在此提交。
您同时也向我们承诺,您提交的内容为您自己所创作,或是复制自公共领域或类似自由来源。
未经许可,请勿提交受著作权保护的作品!
取消
编辑帮助
(在新窗口中打开)