c++字符串string寻找子字符的位置find、rfind、find_first_of、find_last_of

本文介绍了C++中四个重要的字符串查找函数:find、find_last_of、find_first_of和find_last_of,详细阐述了它们的功能和用法。通过示例代码展示了如何在字符串中查找指定子串或字符,并返回匹配的位置。对于没有找到匹配的情况,函数会返回string::npos。
  1. size_t find (const string& str, size_t pos = 0)
    说明:在字符串里面找到第一个匹配项,并返回位置。如果没有找到匹配项,该函数将返回string::npos。
#include <iostream>
#include <string>

int main ()
{
  std::string str ("123456789.");
  std::size_t found = str.find("5");
  if (found!=std::string::npos)
    std::cout << found << '\n';
  return 0;
}

2.size_t rfind (const string& str, size_t pos = npos)
说明:在字符串中搜索由其参数str指定的序列的最后一个匹配项。如果没有找到匹配项,该函数将返回string::npos。

#include <iostream>
#include <string>

int main ()
{
  std::string str ("1234568888889.");
  std::size_t found = str.find("8");
  if (found!=std::string::npos)
    std::cout << found << '\n';
  return 0;
}

3.size_t find_first_of (const string& str, size_t pos = 0)
说明:在字符串中搜索与其参数str中指定的任意字符匹配的第一个字符。如果没有找到匹配项,该函数将返回string::npos。

4.size_t find_last_of (const string& str, size_t pos = npos)
说明:在字符串中搜索与其参数str中指定的任意字符匹配的最后一个字符。如果没有找到匹配项,该函数将返回string::npos。

<think> 我们被要求解释stoi函数和string.find_last_of(),并且最后提出3-5个相关问题,以“§§相关问题§§”开头,不能换行分散。 首先,我们分别解释这两个函数: 1. stoi函数: - 在C++中,`std::stoi` 是字符串转整数的函数,定义在`<string>`头文件中。 - 函数原型:`int stoi (const string& str, size_t* idx = 0, int base = 10);` - 参数说明: - str:要转换的字符串。 - idx:一个可选指针,用于存储转换结束的位置(即第一个无法被转换的字符位置)。如果不需要,可以设置为nullptr。 - base:进制数,范围在2~36之间。例如,10表示十进制,16表示十六进制。 - 功能:将字符串中的数字部分转换为整数,直到遇到非数字字符(根据进制判断)或字符串结束。 - 例: ```cpp #include <string> #include <iostream> using namespace std; int main() { string s1 = "123hello"; int num1 = stoi(s1); // num1 = 123 string s2 = "1010"; int num2 = stoi(s2, nullptr, 2); // num2 = 10(二进制转十进制) } ``` 2. string.find_last_of(): - 这是C++标准库中std::string的成员函数,用于查找字符串中最后一个与指定字符集合中任意字符匹配的位置。 - 函数原型:`size_t find_last_of (const string& str, size_t pos = npos) const;` - str:包含要查找字符集合的字符串。 - pos:开始查找位置(从该位置向前查找),默认为npos(直到字符串开头)。 - 返回值:找到的最后一个匹配字符位置(下标),如果没有找到,则返回string::npos。 - 功能:从字符串的指定位置向前搜索,返回最后一次出现指定字符集中任一字符位置。 - 例: ```cpp #include <string> #include <iostream> using namespace std; int main() { string s = "/home/user/file.txt"; // 查找最后一个目录分隔符或扩展名分隔符 size_t found = s.find_last_of("/."); if (found != string::npos) cout << "Found at: " << found << ", character: " << s[found] << endl; // 输出:Found at: 14, character: . (因为它先找到点,然后才可能找到斜杠,但注意是从后往前找) } ``` 注意:这里查找的是任意一个字符最后一次出现的位置。如果字符串中有多个字符查找集中,则返回最后一次出现的位置。 现在,我们按照要求,在最后提出3-5个相关问题: §§相关问题§§ 1. stoi函数在转换过程中遇到非数字字符会如何处理?如何避免转换错误? 2. 如何使用find_last_of函数来提取文件路径中的文件名(不含路径)? 3. stoi函数在处理不同进制时,对字符串中的字母大小写有要求吗? 4. find_last_ofrfind有什么区别?请举例说明。 5. 在使用stoi转换大数字时,如果超出int范围会怎样?如何避免? </think> ### C++ 中的 `std::stoi` 和 `std::string::find_last_of` 详解 #### 1. `std::stoi`:字符串转整数函数 ```cpp #include <string> using namespace std; int main() { // 基础用法:十进制转换 string numStr = "123"; int num = stoi(numStr); // num = 123 // 指定进制转换(支持2~36进制) string hexStr = "A5"; int hexNum = stoi(hexStr, nullptr, 16); // hexNum = 165(十六进制A5转十进制) // 带错误检测的转换 string mixed = "42abc"; size_t pos; // 存储转换结束位置 int value = stoi(mixed, &pos, 10); // value=42, pos=2(停在字母a的位置) } ``` **核心特性:** - **参数说明**: - `const string& str`:输入字符串 - `size_t* idx`:可选指针,存储无效字符位置 - `int base=10`:进制基数(2~36),例如: - 二进制:`base=2` → 只接受 `0-1` - 十六进制:`base=16` → 接受 `0-9, A-F/a-f` - **错误处理**: - 转换失败时抛出 `invalid_argument` 异常 - 数值超出范围时抛出 `out_of_range` 异常 - **注意事项**: 1. 忽略前导空格(如 `" 123"` → `123`) 2. 遇到非法字符立即停止(如 `"12x34"` → `12`) 3. 不处理后缀字符(如 `"56kg"` → `56`) --- #### 2. `std::string::find_last_of`:查找最后出现的字符 ```cpp #include <string> #include <iostream> using namespace std; int main() { string path = "/home/user/docs/report.txt"; // 查找最后一个目录分隔符 size_t dirPos = path.find_last_of('/'); // dirPos = 17(指向最后一个'/'位置) // 提取文件名:从分隔符后到末尾 string filename = path.substr(dirPos + 1); // "report.txt" // 查找文件扩展名分隔符 size_t dotPos = path.find_last_of('.'); // dotPos = 23(指向最后一个'.'位置) // 组合使用:提取不包含扩展名的文件名 string basename = path.substr(dirPos + 1, dotPos - dirPos - 1); // "report" } ``` **核心特性:** - **函数原型**: ```cpp size_t find_last_of (const string& chars, size_t pos = npos) const noexcept; ``` - **参数说明**: - `chars`:目标字符集合,匹配任意一个字符 - `pos`:起始搜索位置(默认为 `npos`,即字符串末尾) - **返回值**: - 返回匹配字符的 **位置索引**(从0开始) - 未找到时返回 `string::npos`(通常为 `-1` 或 `size_t` 最大值) - **典型应用场景**: 1. 文件路径解析(提取文件名/扩展名) 2. 日志分析(查找最后出现的分隔符) 3. 数据清洗(定位末尾特殊字符) --- #### 对比总结 | 特性 | `std::stoi` | `std::string::find_last_of` | |------------------|--------------------------------|----------------------------------| | **功能** | 字符串转整数 | 查找字符集合最后出现的位置 | | **返回类型** | `int` | `size_t`(位置索引) | | **错误处理** | 抛出异常 | 返回 `npos`(需手动检查) | | **时间复杂** | O(n) | O(n) | | **常用场景** | 数字解析/进制转换 | 路径处理/分隔符定位 | | **关联函数** | `stol`, `stoul`, `stof` | `find_first_of`, `rfind` |
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值