C++string中用于查找的find系列函数浅析
2017-03-28 20:39
267 查看
C++string中用于查找的find系列函数浅析
总述:以下所讲的所有的string查找函数,都有唯一的返回类型,那就是size_type,即一个无符号整数(按打印出来的算)。若查找成功,返回按查找规则找到的第一个字符或子串的位置;若查找失败,返回npos,即-1(打印出来为4294967295)。
1.fine()原型:
//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;
#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 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; }
//找出字符串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
2.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()很相似,差别在于查找顺序不一样,rfind()是从指定位置起向前查找,直到串首。例如,上例中的st1.rfind('a',7)一句,就是从st1的位置7(st1的最后一个字符b)开始查找字符a,第一次找到的是倒数第2个字符a,所以返回6。
关于rfind(),不再详述,读者可根据find()的示例,自行写代码学习rfind()。
其他剩下内容可点击此链接点击打开链接
相关文章推荐
- C++string中用于查找的find系列函数浅析
- C++string中用于查找的find系列函数浅析
- C++string中用于查找的find系列函数浅析
- C++ find()函数用法(一般用于vector的查找)
- C++string中的查找函数find与find_first_of函数的区别
- 全国计算机等级二级C++第四章习题编程题6 :编写一个函数 char *strfind(char *s, char *t),用于查找字符串t在字符串s中最右边出现的位置,如果没有找到则返回NULL.
- 编写一个函数 char *strfind(char *s, char *t),用于查找字符串t在字符串s中最右边出现的位置,如果没有找到则返回NULL.
- C++string中有关字符串内容修改和替换的函数浅析
- STL(1)——查找函数find的使用
- oracle分析函数系列之first_value/last_value:在记录集中查找第一条记录和最后一条记录
- Linux常用自定义脚本系列(find-in-jar) - 在当前目录中查找包含着某个类的所有jar文件
- va_list、va_start、va_end(用于实现函数参数的个数可变的函数,如scanf()/printf()系列函数)
- 【涨函数】std::find,std::find_if对类进行查找
- 用window api 函数findfirstfile来查找
- find_if和函数对象结合实现对自定义类型vector的查找 复杂结构体查询某个值
- 浅析busybox查找命令和调用相应命令函数的实现流程框架
- Lua 字符串查找函数 string.find(s, pattern [, init [, plain]] )
- 在一个char数组中查找指定的字符ch,函数名findchar
- C++查找文件函数FindFirstFile
- memset()函数用法浅析----笛风读书笔记系列