std::string find 的返回值

本文详细介绍了C++中std::string类的find方法的使用方式及注意事项,包括如何正确判断查找结果以及find系列方法的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

转自:https://www.cnblogs.com/pjl1119/p/8676647.html

std::string  的方法 find,返回值类型是std::string::size_type, 对应的是查找对象在字符串中的位置(从0开始),

如果未查找到,该返回值是一个很大的数据(4294967295),判断时与 std::string::npos 进行对比

std::string str("abcdefg");
std::string::size_type pos = str.find("abc");
if (pos != std::string::npos)
{
    cout << "Not find" << endl;
}

std::string str("abcdefg");
if (str.find("abc") != std::string::npos)
{
    cout << "Not find" << endl;
}

 

很多同学由于经常使用 CString 的缘故,喜欢这样写

std::string str("abcdefg");
int pos = str.find("abc");
if (pos < 0)
{
    cout << "Not find" << endl;
}

这样写理论上也是可以的,因为 size_type 相当于 unsigned int类型,最大值4294967295强制转换为int型,就是-1

但下面的写法是错误的:

std::string str("abcdefg");
if (str.find("abc") < 0)  //错误,应该写成  != std::string::npos
{
    cout << "Not find" << endl;
}
最后,建议使用size_type,这样可以适应不同的平台。因为int 类型的大小会根据不同平台而不同。


拓展:
s.find(s2)        第一次出现的位置
s.rfind           最后一次出现的位置
s.find_first_of(s2)    任何一个字符第一次出现的位置
s.find_last_of         任何一个字符最后一次出现的位置
s.find_first_not_of(s2)   第一个不在s2中的字符所在位置
s.find_last_not_of(s2)    最后一个不在s2中的字符所在位置
### C++ 中 `std::string::find` 方法的使用指南 `std::string::find` 是 C++ 标准库中用于在字符串中查找子字符串或字符的方法。它提供了多种重载形式,可以满足不同的查找需求[^4]。 #### 函数原型 以下是 `std::string::find` 的主要函数原型: ```cpp size_t find(const 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; ``` #### 参数说明 - `str`:要查找的子字符串。 - `s`:要查找的 C 风格字符串。 - `c`:要查找的单个字符。 - `pos`:开始查找的起始位置,默认为 0。 #### 返回值 如果找到目标字符串或字符,`std::string::find` 返回其第一次出现的位置(从 0 开始计数)。如果未找到,则返回 `std::string::npos`,这是一个特殊的常量,表示查找失败[^4]。 #### 示例代码 以下是一些常见的用法示例: ##### 查找子字符串 ```cpp #include <iostream> #include <string> int main() { std::string str = "Hello, world!"; std::string sub = "world"; size_t found = str.find(sub); if (found != std::string::npos) { std::cout << "Substring found at position: " << found << std::endl; } else { std::cout << "Substring not found" << std::endl; } return 0; } ``` ##### 查找单个字符 ```cpp #include <iostream> #include <string> int main() { std::string str = "Hello, world!"; char c = 'o'; size_t found = str.find(c); if (found != std::string::npos) { std::cout << "Character found at position: " << found << std::endl; } else { std::cout << "Character not found" << std::endl; } return 0; } ``` ##### 指定起始位置查找 ```cpp #include <iostream> #include <string> int main() { std::string str = "Hello, world!"; char c = 'o'; size_t pos = 5; size_t found = str.find(c, pos); if (found != std::string::npos) { std::cout << "Character found at position: " << found << std::endl; } else { std::cout << "Character not found after position " << pos << std::endl; } return 0; } ``` #### 注意事项 1. 如果需要查找所有匹配项,可以通过循环调用 `find` 并更新起始位置来实现[^4]。 2. 使用 `std::string::npos` 判断查找结果时,需确保正确处理未找到的情况。 3. `std::string::find` 是区分大小写的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值