概述
c++中string的find函数大致可以有:find(),rfind(),find_first_of(),find_last_of(),find_first_not_of(),find_last_not_of()这六种。接下来我将从这六个方法进行展开介绍。
详解
find()函数
- 函数原型:
//string (1)
size_type find (const basic_string& str, size_type pos = 0) const noexcept;
//c-string (2)
size_type find (const charT* s, size_type pos = 0) const;
//buffer (3)
size_type find (const charT* s, size_type pos, size_type n) const;
//character (4)
size_type find (charT c, size_type pos = 0) const noexcept;
- 原型解释(假设ss为调用函数的string变量)
- 原型1.
- 从ss中位置为pos处开始查找字符串str。pos默认为0;
- 原型2
- 从ss中位置为pos出开始查找指针s所指向的字符串。pos默认为0;
- 原型3
- 从ss中位置为pos出开始查找指针s所指向数组的前n个字符。pos与n都没有默认值;
- 原型4
- 从ss中位置为pos处开始查找字符c。pos默认为0;
- 范例
int main()
{
string str("babccbabcaabcccbabccabcabcabbabcc");
//1.size_type find (const basic_string& str, size_type pos = 0) const noexcept;
string tem = "abc";
cout << str.find(tem) << endl; //1
cout << str.find(tem, 2) << endl; //6
//2.size_type find (charT c, size_type pos = 0) const noexcept;
cout << str.find(‘c’) << endl; //3
cout << str.find(‘c’, 4) << endl; //4
//3.size_type find (const c