C++中string的find函数

 参考:博客园 同勉共进

find函数的返回值是无符号整型,如果查找不到,就会返回4294967295(string::npos表示-1或4294967295)

/*测试string中的find*/ 
#include<iostream>
#include<string>

using namespace std;

int main()
{
    //测试size_type find (charT c, size_type pos = 0) const noexcept;
    string st1("babbabab");
    cout << st1.find('a') << endl;//1   由原型知,若省略第2个参数,则默认从位置0(即第1个字符)起开始查找
    cout << st1.find('a', 0) << endl;//1
    cout << st1.find('a', 1) << endl;//1   
    cout << st1.find('a', 2) << endl;//4   在st1中,从位置2(b,包括位置2)开始,查找字符a,返回首次匹配的位置,若匹配失败,返回npos
    cout << st1.rfind('a',7) << endl;//6   关于rfind,后面讲述
    cout << st1.find('c', 0) << endl;//4294967295
    cout << (st1.find('c', 0) == -1) << endl;//1 string:npos 
    cout << (st1.find('c', 0) == 4294967295) << endl;//1   两句均输出1,原因是计算机中-1和4294967295都表示为32个1(二进制)
    cout << st1.find('a', 100) << endl;//4294967295   当查找的起始位置超出字符串长度时,按查找失败处理,返回npos
    //测试size_type find (const basic_string& str, size_type pos = 0) const noexcept;
    string st2("aabcbcabcbabcc");
    string str1("abc");
    cout << st2.find(str1, 2) << endl;//6   从st2的位置2(b)开始匹配,返回第一次成功匹配时匹配的串(abc)的首字符在st2中的位置,失败返回npos
    //测试size_type find (const charT* s, size_type pos = 0) const;
    cout << st2.find("abc", 2) << endl; //6   同上,只不过参数不是string而是char*
    //测试size_type find (const charT* s, size_type pos, size_type n) const;
    cout << st2.find("abcdefg", 2, 3) << endl;//6   取abcdefg得前3个字符(abc)参与匹配,相当于st2.find("abc", 2)
    cout << st2.find("abcbc", 0, 5) << endl;//1   相当于st2.find("abcbc", 0)
    cout << st2.find("abcbc", 0, 6) << endl;//4294967295   第3个参数超出第1个参数的长度时,返回npos
    return 0;
}

 

### C++ `string` 类中的 `find` 函数C++ 的标准库中,`std::string` 提供了一个名为 `find` 的成员函数。该函数用于在一个字符串中查找子串的位置。如果找到目标子串,则返回其首次出现的第一个字符索引;如果没有找到,则返回特殊值 `std::string::npos`。 #### 返回值类型 需要注意的是,`find` 函数的返回值是一个无符号整数类型 `size_t`,因此应该用 `size_t` 类型的变量来存储返回的结果[^2]。 #### 基本语法 以下是 `find` 函数的基本形式及其参数描述: ```cpp size_t find(const string& str, size_t pos = 0) const; ``` - **str**: 要查找的目标子串。 - **pos**(可选): 开始查找的位置,默认为 0 表示从头开始查找。 #### 示例代码 下面展示如何使用 `find` 函数以及处理未找到的情况: ```cpp #include <iostream> #include <string> using namespace std; int main() { string s1 = "Hello World Hello World"; // 查找子串 "World" size_t find_pos = s1.find("World"); if (find_pos != string::npos) { cout << "子串 'World' 首次出现在位置:" << find_pos << endl; } else { cout << "'World' 子串不存在于原字符串中" << endl; } // 尝试查找不存在的子串 "abc" size_t not_found_pos = s1.find("abc"); if (not_found_pos == string::npos) { cout << "子串 'abc' 不存在于原字符串中" << endl; } return 0; } ``` 运行上述程序会输出如下结果: ``` 子串 'World' 首次出现在位置:6 子串 'abc' 不存在于原字符串中 ``` #### 特殊情况解析 当调用 `find` 方法而未能匹配到任何子串时,它将返回常量 `std::string::npos`。这是一个特殊的标记值,表示“未找到”。此值实际上是 `size_t` 类型的最大可能值。 #### 扩展功能 除了基本的 `find` 外,还有其他类似的变体可以满足不同的需求,比如: - `find_first_of`: 寻找任意属于给定集合的一个字符第一次出现的位置[^3]。 - `find_last_of`: 和 `find_first_of` 功能相同,只是方向相反,即从右向左寻找最后一个符合条件的字符。 - `find_first_not_of`: 寻找第一个不属于某个特定集合的字符所在位置。 - `find_last_not_of`: 同样是从右侧起查找出最后一位不包含在内的字符位置[^4]。 #### 总结 通过以上介绍可以看出,在实际开发过程中合理运用这些方法能够极大地简化涉及字符串操作的任务,并提高效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值