string find函数



概述
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 charT* s, size_type pos, size_type n) const;
cout << str.find("abcdef", 2, 3) << endl; //6

//4.size_type find (const charT* s, size_type pos = 0) const;
cout << str.find("abc", 2) << endl; //6
return 0;

}

rfind()函数
  • 函数模型
//string (1)

size_type rfind (const basic_string& str, size_type pos = npos) const noexcept;

//c-string (2)

size_type rfind (const charT* s, size_type pos = npos) const;

//buffer (3)

size_type rfind (const charT* s, size_type pos, size_type n) const;

//character (4)

size_type rfind (charT c, size_type pos = npos) const noexcept;


  • 原型解释
rfind()与find()函数的很相似,他们的差别就在于查找的方向不同。find()函数时从前往后进行查找的,而rfind()函数时从后向前查找的。
  • 范例
int main()
{
string str("babccbabcaabcccbabccabcabcabbabcc");
//1.size_type find (const basic_string& str, size_type pos = 0) const noexcept;
string tem = "abc";
cout << str.rfind(tem) << endl; //29
cout << str.rfind(tem, 2) << endl;//1

//2.size_type find (charT c, size_type pos = 0) const noexcept;
cout << str.rfind(‘c’) << endl; //32
cout << str.rfind(‘c’, 10) << endl; //8

//3.size_type find (const charT* s, size_type pos, size_type n) const;
cout << str.rfind("abcdef", 10, 3) << endl; //10

//4.size_type find (const charT* s, size_type pos = 0) const;
cout << str.rfind("abc", 20) << endl; //20
return 0;

}

find_first_of()函数
  • 函数模型
//string (1)

size_type find_first_of (const basic_string& str, size_type pos = 0) const noexcept;

//c-string (2)

size_type find_first_of (const charT* s, size_type pos = 0) const;

//buffer (3)

size_type find_first_of (const charT* s, size_type pos, size_type n) const;

//character (4)

size_type find_first_of (charT c, size_type pos = 0) const noexcept;


  • 原型解释:有find_firdt_of()函数的参数列表可以知道其参数与find()函数的参数列表是相同的。所以原型的作用也与find()函数的作用相似。 查找字符串中字符c、字符数组中任意一个字符第一次出现的位置。也就是说find()函数使找到与实参完全相同的字符或者字符串,但是find_first_of()是需要查找实参送出现的任意字符就行。
  • 范例
int main()
{
string str("babccbabcaabcccbabccabcabcabbabcc");
cout << str.find_first_of("fjnmnag") << endl; //输出1 find_first_of()查找到字符串中a造str中第一次出现的下标
cout << str.find("fjnmnag") << endl; // 输出4294967295 表示没有找到
return 0;

}

find_last_of()函数
  • 函数模型
//string (1)

size_type find_last_of (const basic_string& str, size_type pos = npos) const noexcept;

//c-string (2)

size_type find_last_of (const charT* s, size_type pos = npos) const;

//buffer (3)

size_type find_last_of (const charT* s, size_type pos, size_type n) const;

//character (4)

size_type find_last_of (charT c, size_type pos = npos) const noexcept;

  • 原型解释:与find_first_of()相反。这个函数是查找最后一次出现的位置。
  • 范例
int main()
{
string str("babccbabcaabcccbabccabcabcabbabcc");
cout << str.find_last_of("fjnmnag") << endl; //输出29 find_first_of()查找到字符串中a造str中第一次出现的下标
cout << str.find("fjnmnag") << endl; // 输出4294967295 表示没有找到

return 0;

}
find_first_not_of()函数
  • 函数模型
//string (1)

size_type find_first_not_of (const basic_string& str, size_type pos = 0) const noexcept;

//c-string (2)

size_type find_first_not_of (const charT* s, size_type pos = 0) const;

//buffer (3)

size_type find_first_not_of (const charT* s, size_type pos, size_type n) const;

//character(4)

size_type find_first_not_of (charT c, size_type pos = 0) const noexcept;

  • 原型解释:其中的参数列表与之前的完全一样,函数的意思在源串中从位置pos开始往后查找,只要在源串遇到一个字符,该字符与目标串中的任意一个字符都不相同,就停止查找,返回该字符在源串中的位置;若遍历完整个源串,都找不到满 足条件的字符,则返回npos。
  • 范例
int main()
{
string str("babccbabcaabcccbabccabcabcabbabcc");
string tem = "abc";
cout << str.find_first_not_of("fjnmnag") << endl; //输出0,因为第一个b就不存在目标中,所以第一个就符合条件。
cout << str.find_first_not_of("fjnmnbg") << endl; //输出0,因为第一个b就在目标中存在。
cout << str.find_first_not_of("fjnmnag",1) << endl; //输出为2,期目标位置就是从下标1开始的,a是符合条件的,而之后的b是不符合条件的。
cout << str.find_first_not_of(tem) << endl;
return 0;

}

find_last_not_of()函数
  • 函数模型
//string (1)

size_type find_last_not_of (const basic_string& str, size_type pos = npos) const noexcept;

//c-string (2)

size_type find_last_not_of (const charT* s, size_type pos = npos) const;

//buffer (3)

size_type find_last_not_of (const charT* s, size_type pos, size_type n) const;

//character (4)

size_type find_last_not_of (charT c, size_type pos = npos) const noexcept;

  • 原型解释:与前面的函数有着相同的作用只是其查找位置不同,find_last_not_of()是从后向前开始查找的。
  • 范例
int main()
{
string str("babccbabcaabcccbabccabcabcabbabcc");
string tem = "abc";
cout << str.find_last_not_of("fjnmnag") << endl;
cout << str.find_last_not_of("fjnmnbg") << endl;
cout << str.find_last_not_of("fjnmnag",1) << endl;
cout << str.find_last_not_of(tem) << endl;
return 0;

}
函数原型摘自博客 http://www.cnblogs.com/zpcdbky/p/4471454.html

你可能感兴趣的:(c++)