C++string中用于查找的find系列函数浅析
总述:
以下所讲的所有的string查找函数,都有唯一的返回类型,那就是size_type,即一个无符号整数(按打印出来的算)。若查找成功,返回按查找规则找到的第一个字符或子串的位置;若查找失败,返回npos,即-1(打印出来为4294967295)。
1.find()
原型:
//string (1)
size_type find (const basic_string& str, size_type pos = ) const noexcept;
//c-string (2)
size_type find (const charT* s, size_type pos = ) 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 = ) 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', ) << endl;//
cout << st1.find('a', ) << endl;//1
cout << st1.find('a', ) << endl;//4 在st1中,从位置2(b,包括位置2)开始,查找字符a,返回首次匹配的位置,若匹配失败,返回npos
cout << st1.rfind('a',) << endl;//6 关于rfind,后面讲述
cout << st1.find('c', ) << endl;//
cout << (st1.find('c', ) == -) << endl;//
cout << (st1.find('c', ) == ) << endl;//1 两句均输出1,原因是计算机中-1和4294967295都表示为32个1(二进制)
cout << st1.find('a', ) << 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, ) << endl;//6 从st2的位置2(b)开始匹配,返回第一次成功匹配时匹配的串(abc)的首字符在st2中的位置,失败返回npos
//测试size_type find (const charT* s, size_type pos = 0) const;
cout << st2.find("abc", ) << endl; //6 同上,只不过参数不是string而是char*
//测试size_type find (const charT* s, size_type pos, size_type n) const;
cout << st2.find("abcdefg", , ) << endl;//6 取abcdefg得前3个字符(abc)参与匹配,相当于st2.find("abc", 2)
cout << st2.find("abcbc", , ) << endl;//1 相当于st2.find("abcbc", 0)
cout << st2.find("abcbc", , ) << endl;//4294967295 第3个参数超出第1个参数的长度时,返回npos
return ;
}
应用举例:
//找出字符串str中所有的"abc"(输出位置),若未找到,输出"not find!"
#include<iostream>
#include<string> using namespace std; int main()
{
string str("babccbabcaabcccbabccabcabcabbabcc");
int num = ;
size_t fi = str.find("abc", );
while (fi!=str.npos)
{
cout << fi << " ";
num++;
fi = str.find("abc", fi + );
}
if ( == num)
cout << "not find!";
cout << endl;
return ;
}
//运行结果:
//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()。
3.find_first_of()
原型:
//string (1)
size_type find_first_of (const basic_string& str, size_type pos = ) const noexcept;
//c-string (2)
size_type find_first_of (const charT* s, size_type pos = ) 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 = ) const noexcept;
说明:
在源串中从位置pos起往后查找,只要在源串中遇到一个字符,该字符与目标串中任意一个字符相同,就停止查找,返回该字符在源串中的位置;若匹配失败,返回npos。
示例(仅给出部分原型的实例,对于其余原型,相信读者有能力仿照前边关于find()的实例来自行写代码测试和学习):
#include<iostream>
#include<string> using namespace std; int main()
{
//测试size_type find_first_of (charT c, size_type pos = 0) const noexcept;
string str("babccbabcc");
cout << str.find('a', ) << endl;//
cout << str.find_first_of('a', ) << endl;//1 str.find_first_of('a', 0)与str.find('a', 0)
//测试size_type find_first_of (const basic_string& str, size_type pos = 0) const noexcept;
string str1("bcgjhikl");
string str2("kghlj");
cout << str1.find_first_of(str2, ) << endl;//从str1的第0个字符b开始找,b不与str2中的任意字符匹配;再找c,c不与str2中的任意字符匹配;再找g,
//g与str2中的g匹配,于是停止查找,返回g在str1中的位置2
//测试size_type find_first_of (const charT* s, size_type pos, size_type n) const;
cout << str1.find_first_of("kghlj", , );//2 尽管第3个参数超出了kghlj的长度,但仍能得到正确的结果,可以认为,str1是和"kghlj+乱码"做匹配
return ;
}
应用举例:
//将字符串中所有的元音字母换成*
//代码来自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 + );
}
std::cout << str << '\n';
return ;
}
//运行结果:
//PL**s* r*pl*c* th* v*w*ls *n th*s s*nt*nc* by *st*r*sks
4.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()函数相似,只不过查找顺序是从指定位置向前,这里仅简单举例,不再赘述,读者可参考find_first_of()自行学习。
示例:
#include<iostream>
#include<string> using namespace std; int main()
{
//测试size_type find_last_of (const charT* s, size_type pos = npos) const;
//目标串中仅有字符c与源串中的两个c匹配,其余字符均不匹配
string str("abcdecg");
cout << str.find_last_of("hjlywkcipn", ) << endl;//5 从str的位置6(g)开始想前找,g不匹配,再找c,c匹配,停止查找,返回c在str中的位置5
cout << str.find_last_of("hjlywkcipn", ) << endl;//2 从str的位置4(e)开始想前找,e不匹配,再找d,d不匹配,再找c,c匹配,停止查找,
// 返回c在str中的位置5
cout << str.find_last_of("hjlywkcipn", ) << endl;//5 当第2个参数超出源串的长度(这里str长度是7)时,不会出错,相当于从源串的最后一
// 个字符起开始查找
return ;
}
5.find_first_not_of()
原型:
//string (1)
size_type find_first_not_of (const basic_string& str, size_type pos = ) const noexcept;
//c-string (2)
size_type find_first_not_of (const charT* s, size_type pos = ) 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 = ) const noexcept;
说明:
在源串中从位置pos开始往后查找,只要在源串遇到一个字符,该字符与目标串中的任意一个字符都不相同,就停止查找,返回该字符在源串中的位置;若遍历完整个源串,都找不到满 足条件的字符,则返回npos。
示例(仅简单举例,有了前边的学习,相信读者可以自己学习find_first_not_of()):
#include<iostream>
#include<string> using namespace std; int main()
{
//测试size_type find_first_not_of (const charT* s, size_type pos = 0) const;
string str("abcdefg");
cout << str.find_first_not_of("kiajbvehfgmlc", ) << endl;//3 从源串str的位置0(a)开始查找,目标串中有a(匹配),再找b,b匹配,再找c,c匹配,
// 再找d,目标串中没有d(不匹配),停止查找,返回d在str中的位置3
return ;
}
将前面应用举例中的fing_first_of()换成find_first_not_of(),就可以将字符串中所有非元音字母换成*。请读者自己验证。
6.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()与find_first_not_of()相似,只不过查找顺序是从指定位置向前,这里不再赘述,相信读者可以自行学习。
C++string中用于查找的find系列函数浅析的更多相关文章
- C++string中有关大小和容量的函数浅析
1.length()与size() length是因为沿用C语言的习惯而保留下来的,string类最初只有length,引入STL之后,为了兼容又加入了size,它是作为STL容器的属性存在的,便于符 ...
- python中实现查找字符串的find函数
第五题:自己实现一个字符串的find函数1.在一个字符串中查找另一个字符串2.找到了返回第一次出现的位置3.没找到返回-14.参数s1为源字符串,参数s2为要查找的字符串 def index_of_s ...
- C语言中用于计算数组长度的函数 “strlen() ”。
de>#include<stdio.h>#include<stdlib.h>#define MAX_LEN 255int my_strlen1(const char* s ...
- sql server 中用于排名的三个函数 row_number() ,RANK() 和 DENSE_RANK()
row_number() ,RANK() 和 DENSE_RANK() 三个配合over() 使用排名 只是只是计算方式不一样,语法基本一样 语法: ROW_NUMBER() OVER (OR ...
- php中urlencode()和urldecode()URL编码函数浅析[转]
URLEncode:是指针对网页url中的中文字符的一种编码转化方式,最常见的就是Baidu.Google等搜索引擎中输入中文查询时候,生成经过Encode过的网页URL.URLEncode的方式一般 ...
- python中的作用域以及内置函数globals()-全局变量、locals()-局部变量
在python中,函数会创建一个自己的作用域,也称为为命名空间.这意味着在函数内部访问某个变量时,函数会优先在自己的命名空间中寻找. 通过内置函数globals()返回的是python解释器能知道的变 ...
- Java-J2SE学习笔记-查找一个String中,subString的出现次数
1.查找一个String中,subString的出现次数 2.代码 package Test; public class TestStringContain { public static void ...
- 【正则表达式】使用正则表达式的group,查找出String中的参数值
需求 指标基本格式: clm.{type}.{hostId}.$metricItem 示例1: // 待匹配表达式:<hostId: 为36位的UUID> summarize(clm.pm ...
- Java基础扫盲系列(-)—— String中的format
Java基础扫盲系列(-)-- String中的format 以前大学学习C语言时,有函数printf,能够按照格式打印输出的内容.但是工作后使用Java,也没有遇到过格式打印的需求,今天遇到项目代码 ...
随机推荐
- cms修改栏目单页样式错位调整
if (dt.Rows[0]["ClassTemplet"].ToString().Trim() == "") { rows_key.Style.Value = ...
- swift 2.x学习笔记(一)
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #008400 } p.p2 { margin: 0.0px 0. ...
- .NET 框架基本原理透析⑴
.NET框架的核心便是通用语言运行时(CLR),顾名思义它是一个可被各种不同的编程语言所使用的运行时.CLR的很多特性可用于所有面向它的编程语言.比如,如果CLR用异常来报告错误,那么所有面向它的语言 ...
- 磨刀不误砍柴工,使用visual studio之前应该先了解这些...
注:以下的快捷键在vs2010中测试无误. 众所周知,vs是一个非常强大的开发平台,但是又有多少小伙伴熟悉以下这些快捷键呢? 当然,不知道这些快捷键与开发并没有直接关系,不过,就我而言,以下这些快捷键 ...
- FIO是测试IOPS
FIO是测试IOPS的非常好的工具,用来对硬件进行压力测试和验证,支持13种不同的I/O引擎,包括:sync,mmap, libaio, posixaio, SG v3, splice, null, ...
- 20145320《Java程序设计》第五次实验报告
20145320<Java程序设计>第五次实验报告 北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1453 指导教师:娄嘉鹏 实验日期:2016.05.06 18: ...
- nmap使用教程
Nmap是一款开源免费的网络发现(Network Discovery)和安全审计(Security Auditing)工具.软件名字Nmap是Network Mapper的简称.Nmap最初是由Fyo ...
- 感冒了~ vs中py和vb实现一个小算法
1+1*2+1*2*3+--+1*2*3*n 下面是窗体,就一个按钮和编辑框. 中途还遇到了编码问题,但是感冒太难受,加上明天还要上课.就睡了~ 晚安世界.
- PHP js使用ajax异步处理方式请求PHP,解决数组中文乱码
html端: <html> <head> <script type="text/javascript" src="jquery/jquery ...
- [转]装完CentOS后,重新开机启动后显示: Initial setup of CentOS Linux 7 (core)
转:装完Centos7提示Initial setup of CentOS Linux 7 (core) 在用U盘装完CentOS后,重新开机启动后显示: Initial setup of Cent ...