**
find系列函数整理笔记
**
总述:
所有的string查找函数,都有唯一的返回类型size_type,即一个无符号整数(按打印出来的算)。若查找成功,返回按查找规则找到的第一个字符或子串的位置;若查找失败,返回npos,即-1(打印出来为4294967295)。
find()
size_type find (charT c, size_type pos = 0) const noexcept;
例:
string s1="aabbccbb";
s1.find('b')//2;若省略第二个参数,默认为从0(第一个字符)开始;
s1.find('b',0)//2;
s1.find('b',4)//6;//从第4个开始找;
s1.find('b',9)//4294967295;查找失败返回npos;
s1.find('m',0)//4294967295;查找失败;
s1.find('m',0)==-1//1;
s1.find('m',0)==4294967295//1;-1和4294967295都表示为32个1(二进制);
size_type find (const basic_string& str, size_type pos = 0) const noexcept;
例:
string s1="acbabcbcabcba";
string s2="ba";
cout<<s1.find(s2,1)<<endl;//2;从s1的第1个位置(c)开始找s2,找到第一次s2的首字符的位置,失败返回npos;
size_type find (const charT* s, size_type pos = 0) const;
例:
string s1="acbcabca";
cout<<s1.find("ab",0)//4;
size_type find (const charT* s, size_type pos, size_type n) const;
例:
string s1="abcabcdfgdfg";
s1.find("abcdfg",0,2)//0;表示取“abcdfg”的前两个字符参与寻找;0表示从0位置开始;
s1.find("abcd",0,8)//4294967295;第三个参数超出了第一个参数的长度,返回npos;
相关例题:
//找出字符串str中所有的"abc"(输出位置),若未找到,输出"not find!"
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str("babccbabcaabcccbabccabcabcabbabcc");
int num = 0;
size_t fi = str.find("abc", 0);
while (fi!=str.npos)
{
cout << fi << " ";
num++;
fi = str.find("abc", fi + 1);
}
if (0 == num)
cout << "not find!";
cout << endl;
return 0;
}
//运行结果:
//1 6 10 16 20 23 29
作为编程新手,下面是蓝桥杯里的一题,涉及find函数,所以找了相关的资料,为更好理解整理作为笔记;
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
string s="223344AA";
do
{
unsigned iab=s.find("A",0);//从第一个位置开始查找“A”,并且返回位置,字符串是从0开始算起;
unsigned iae=s.find("A",iab+1);
unsigned i2b=s.find("2",0);
unsigned i2e=s.find("2",i2b+1);
unsigned i3b=s.find("3",0);
unsigned i3e=s.find("3",i3b+1);
unsigned i4b=s.find("4",0);
unsigned i4e=s.find("4",i4b+1);
/*前面的功能是找出每次的一个新字符串每个字母出现的位置*/
if(iae-iab==2&&i2e-i2b==3&&i3e-i3b==4&&i4e-i4b==5)
{
cout<<s<<endl;
}
/*要求:两个A中间有1张牌,两个2之间有2张牌,两个3之间有3张牌,两个4之间有4张牌。*/
}while(next_permutation(s.begin(),s.end()));//next_permutation()的功能是产生全排列;
}
rfind()
功能:和find()函数类似,rfind()是从指定位置向前查找;
原型:
size_type rfind (const basic_string& str, size_type pos = npos) const noexcept;
size_type rfind (const charT* s, size_type pos = npos) const;
size_type rfind (const charT* s, size_type pos, size_type n) const;
size_type rfind (charT c, size_type pos = npos) const noexcept;
find_first_of()
功能:在源串中从指定位置起往后查找,只要在源串中遇到一个字符,该字符与目标串中任意一个字符相同,就停止查找,返回该字符在源串中的位置;若匹配失败,返回npos。
size_type find_first_of (charT c, size_type pos = 0) const noexcept;
例:
string s1="asdasdasd";
cout<<s1.find('a',0)//0;
size_type find_first_of (const basic_string& str, size_type pos = 0) const noexcept;
例:
string s1="abcabcabc";
string s2="cbds";
cout<<s1.find_first_of(s2,0)<<endl;//1;从0位置开始查找,s1中的字符第一个出现在s2中的是位置为1的“b”;
size_type find_first_of (const charT* s, size_type pos = 0) const;
例:
string s1="abcabcabc";
cout<<s1.find_first_of("cbds",0);//1;
size_type find_first_of (const charT* s, size_type pos, size_type n) const;
例:
string s1="sfdasgadasdd";
cout<<s1.find_first_of("sads",0,100);//0;取第一个参数的前100位,虽然超出了第一个参数的长度,但是会继续查找,内容为第一个参数以及其后面的符号乱码(共100位);
//将字符串中所有的元音字母换成*
//代码来自C++ Reference,地址:http://www.cplusplus.com/reference/string/basic_string/find_first_of/
#include<iostream>
#include<string>
using namespace std;
int main()
{
std::string str("PLease, replace the vowels in this sentence by asterisks.");
std::string::size_type found = str.find_first_of("aeiou");
while (found != std::string::npos)
{
str[found] = '*';
found = str.find_first_of("aeiou", found + 1);
}
std::cout << str << '\n';
return 0;
}
//运行结果:
//PL**s* r*pl*c* th* v*w*ls *n th*s s*nt*nc* by *st*r*sks
find_last_of()
find_last_of()函数和find_first_of()类似,区别在于此函数是从指定位置向前查找,函数原型类似;find_first_of()
功能:在源串中从指定位置开始往后查找,只要在源串遇到一个字符,该字符与目标串中的任意一个字符都不相同,就停止查找,返回该字符在源串中的位置;若遍历完整个源串,都找不到满 足条件的字符,则返回npos。find_last_not_of()
功能:在源串中从指定位置开始往前查找,只要在源串遇到一个字符,该字符与目标串中的任意一个字符都不相同,就停止查找,返回该字符在源串中的位置;若遍历完整个源串,都找不到满 足条件的字符,则返回npos。
本文整理作为学习笔记,如有错误请告知,谢谢;
参考网址:https://www.cnblogs.com/zpcdbky/p/4471454.html