string的搜索操作一共6个

1.find

size_t find (const string& str, size_t pos = 0) const;
//从调用对象的下标pos处开始查找字符串str,找到返回第一次出现str的下标,没找到,返回npossize_t find (const char* s, size_t pos = 0) const;
//从调用对象的下标pos处开始查找字符数组s,找到返回字符数组s第一次出现的下标,否则返回npossize_t find (const char* s, size_t pos, size_t n) const;
//从调用对象的下标pos处查找字符数组的前n个字符,找到返回前n个字符第一次出现的位置,否则返回npossize_t find (char c, size_t pos = 0) const;
//从调用对象的位置pos处开始查找字符c,找到,返回字符c第一次出现的位置,否则返回npos

find函数是只读的,并不会改变调用对象,返回值是对应字符或字符串第一次出现的位置,是一个无符号整形数据

关于npos见博客https://blog.csdn.net/Master_Cui/article/details/107573300,npos是一个static unsigned的类型,值为-1,作为函数的参数表示字符串的结尾,作为返回值,表示没有匹配到字符串

举例

void testfind()
{string s="1234567890";int res=s.find("1234", 5);if (res != string::npos) {cout<<"find"<<endl;}cout<<res<<endl;
}

从字符串s的下标5的位置开始查找字符串1234,没找到,返回-1

void testfind()
{string s="1234567890";size_t res=s.find("1234");if (res != string::npos) {cout<<"find"<<endl;}cout<<res<<endl;
}

此时从头开始找字符串1234,找到,返回第一次出现字符串1234的下标位置0

因为成员函数find的返回值要么是一个大于0的数,要么是string::npos,所以用size_t类型的数据接受没有问题,如果想用int型数据接受,也可以,只不过当查找失败时,string::npos自动转化为-1

但是,绝对不要用0和find的返回值进行比较,因为即使查找失败,返回了string::npos,也是一个很大的数,大于0,无法判断是否查找成功

void testfind()
{string s="1234567890";if (s.find("1234", 3)>0) {cout<<"find"<<endl;}
}

正常来说是没有查找到的,但是将find的返回值和0比较,所以得出了错误的结果,所以要么用int接受find的返回值和string::npos比较,要么用size_t接受find的返回值和string::npos比较,后者更好

find的其他示例

void testfind2()
{string s="1234567890";size_t res=s.find('7', 4);//从下标4开始查找字符'7'if (res != string::npos) {cout<<"find"<<endl;}cout<<res<<endl;
}

void testfind3()
{string s="1234567890";size_t res=s.find("7891111", 4, 3);//从s的下标4位置开始,查找"7891111"的前三个字符组成的字符串if (res != string::npos) {cout<<"find"<<endl;}
}

结果同上

2.rfind

size_t rfind (const string& str, size_t pos = npos) const;
size_t rfind (const char* s, size_t pos = npos) const;
size_t rfind (const char* s, size_t pos, size_t n) const;
size_t rfind (char c, size_t pos = npos) const;

rfind和find相反,默认从尾到头查找,返回值是字符串或者某个字符最后一次出现在调用对象中的位置,参数形式同find

示例

void testrfind()
{string s="1234567890";size_t res=s.rfind("789", 2);//从下标2开始查找"789"在s中最后一次出现的位置(从"123"中查找"789"),没找到if (res !=string::npos) {cout<<"find"<<endl;}cout<<res<<endl;
}

void testrfind()
{string s="1234567890";size_t res=s.rfind("789");//从字符串结尾向前查找"789"最后一次出现在s中的位置if (res !=string::npos) {cout<<"find"<<endl;}cout<<res<<endl;
}

void testrfind2()
{string s="1234567890";size_t res=s.rfind('8', 4);//从下标4开始向前查找'8'最后一次出现在s中的位置(从"12345"中查找字符8),没找到if (res !=string::npos) {cout<<"find"<<endl;}cout<<res<<endl;
}

void testrfind2()
{string s="1234567890";size_t res=s.rfind("345789", 5, 2);//从下标5开始向前查找"34"第一次出现的位置if (res !=string::npos) {cout<<"find"<<endl;}cout<<res<<endl;
}

3.find_first_of

size_t find_first_of (const string& str, size_t pos = 0) const;
size_t find_first_of (const char* s, size_t pos = 0) const;
size_t find_first_of (const char* s, size_t pos, size_t n) const;
size_t find_first_of (char c, size_t pos = 0) const;

该函数的含义是查找字符或 字符串中任意一个字符 在调用对象中第一次出现的位置返回值是位置下标,参数含义同find和rfind

示例

void testfindfirstof()
{string s="1234567890";size_t res=s.find_first_of("5793");
//从s的下标位置0开始查找"3579"中任何一个字符出现在s中的位置,第一个出现的字符是'3',所以输出2if (res!=string::npos) {cout<<"find"<<endl;}cout<<res<<endl;
}

void testfindfirstof()
{string s="1234567890";size_t res=s.find_first_of("5793", 5);
//从s的下标位置5开始查找与"3579"中任何一个字符出现在s中的位置,第一个匹配字符是'7',所以输出下标6if (res!=string::npos) {cout<<"find"<<endl;}cout<<res<<endl;
}

void testfindfirstof()
{string s="1234567890";size_t res=s.find_first_of('3', 5);//从下标5开始查找字符3在s中第一次出现的位置,没有找到if (res!=string::npos) {cout<<"find"<<endl;}cout<<res<<endl;
}

void testfindfirstof()
{string s="1234567890";size_t res=s.find_first_of("5173", 5, 3);
//从下标5开始查找字符串"5173"中前三个字符中的任意一个字符第一次出现在s中的位置,因为字符7第一次出现,所以返回字符7在s中的下标if (res!=string::npos) {cout<<"find"<<endl;}cout<<res<<endl;
}

void testfindfirstof()
{string s="1234567890";size_t res=s.find_first_of("5173", 5, 2);
//从下标5开始查找"5173"中前2个字符中的任意一个字符第一次出现在s中的位置,
//因为"51"中的任何一个字符都没有出现在"67890"中,所以返回nposif (res!=string::npos) {cout<<"find"<<endl;}cout<<res<<endl;
}

4.find_first_not_of

size_t find_first_not_of (const string& str, size_t pos = 0) const;
size_t find_first_not_of (const char* s, size_t pos = 0) const;
size_t find_first_not_of (const char* s, size_t pos, size_t n) const;
size_t find_first_not_of (char c, size_t pos = 0) const;

该函数和find_first_of相反,查找的是调用对象中第一个不在字符或字符串的字符的位置。很绕。。。。。。 参数形式同上

示例

void testfindfirstnotof()
{string s="1234567890";size_t res=s.find_first_not_of("5173");
//从s中查找第一个不在"5173"中的字符的位置,字符2是第一个不在"5173"中的字符,所以返回下标1if (res!=string::npos) {cout<<"find"<<endl;}cout<<res<<endl;
}

void testfindfirstnotof()
{string s="1234567890";size_t res=s.find_first_not_of("5173", 5);
//从s的下标5开始查找第一个不在"5173"中的字符的位置,字符6是第一个不在"5173"中的字符,所以返回字符6在s中的下标if (res!=string::npos) {cout<<"find"<<endl;}cout<<res<<endl;
}

void testfindfirstnotof()
{string s="1234567890";size_t res=s.find_first_not_of('6');//从下标0开始查找第一个不是字符6的字符if (res!=string::npos) {cout<<"find"<<endl;}cout<<res<<endl;
}

void testfindfirstnotof()
{string s="1234567890";size_t res=s.find_first_not_of("456", 3, 2);
//从s的下标3开始查找第一个没有出现在"456"中前两个字符中的字符,因为字符6是第一个没有出现在"45"中的字符,所以返回字符6的下标if (res!=string::npos) {cout<<"find"<<endl;}cout<<res<<endl;
}

5.find_last_of

size_t find_last_of (const string& str, size_t pos = npos) const;
size_t find_last_of (const char* s, size_t pos = npos) const;
size_t find_last_of (const char* s, size_t pos, size_t n) const;
size_t find_last_of (char c, size_t pos = npos) const;

同find_first_of,只不过是逆向查找

6.find_last_not_of

size_t find_last_not_of (const string& str, size_t pos = npos) const;
size_t find_last_not_of (const char* s, size_t pos = npos) const;
size_t find_last_not_of (const char* s, size_t pos, size_t n) const;
size_t find_last_not_of (char c, size_t pos = npos) const;

同find_first_not_of,只不过是逆向查找

find与rfind返回的是字符或字符串整体出现的位置,而后四个函数返回的是字符或字符串中某个字符的位置

string的数值转化函数

1.各种数值转字符串

string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);

请自行品尝

2.字符串转各种数值

double stod (const string&  str, size_t* idx = 0);
float stof (const string&  str, size_t* idx = 0);
int stoi (const string&  str, size_t* idx = 0, int base = 10);
long stol (const string&  str, size_t* idx = 0, int base = 10);
long double stold (const string&  str, size_t* idx = 0);
long long stoll (const string&  str, size_t* idx = 0, int base = 10);
unsigned long stoul (const string&  str, size_t* idx = 0, int base = 10);
unsigned long long stoull (const string&  str, size_t* idx = 0, int base = 10);

示例

void testtofloat()
{string strfloat="2.33333, a funny num";string::size_type sz;float f=stof(strfloat, &sz);cout<<f<<sz<<strfloat[sz]<<endl;
}

在调用stof后,sz的值变为第一个没有参与数值转化的字符的下标,这里第一个没有参加转化的字符为逗号,所以sz的值变为,的下标

void testtointeger()
{string binstr="101010";string hexstr="4d56";string octstr="123";string autostr1="0123";string autostr2="0xefef";int binint=stoi(binstr, nullptr, 2);//将101010看做二进制数值转为十进制cout<<binint<<endl;int hexint=stoi(hexstr, nullptr, 16);//将4d56看做十六进制数转为十进制cout<<hexint<<endl;int octint=stoi(octstr, nullptr, 8);//将123看做八进制数转为十进制cout<<octint<<endl;int octnum=stoi(autostr1, nullptr, 0);//根据autostr1的格式自动转化为十进制数,因为0123是0开头,所以将0123视为八进制数cout<<octnum<<endl;int hexnum=stoi(autostr2, nullptr, 0);//根据autostr2的格式自动将字符串转为十进制数,因为0xefef以0x开头,所以将0xefef视为十六进制数cout<<hexnum<<endl;
}

当把字符串转为整数时,最后一个参数表示字符串所代表数字的进制数,默认是十进制,也可以修改为其他进制

void testtointeger2()
{string str1="0x789";cout<<stoi(str1)<<endl;//将0x789看做十进制数,按照十进制转化,但是字符x并不是十进制数,所以只转化0,输出0string str2="789";string::size_type sz;cout<<stoi(str2, &sz, 8)<<endl;//将0x789看做八进制数,按照十进制转化,但是8并不是八进制数,所以值转化7,输出7cout<<sz<<endl;//输出第一个没有参加数值转化的字符的下标,这里第一个没有参加数值转化的字符是8,所以输出8的下标string str4="   77";//去掉前面的空格,输出77cout<<stoi(str4)<<endl;string str3=". 77";//无法转化,抛出异常cout<<stoi(str3)<<endl;
}

通过上述输出可知,当遇到无法转化的字符串时,函数会抛出异常,当字符串前面含有空格时,函数会去掉前面的空格,再尝试转化

当开始转化时,会逐个字符读取,当字符所代表的数值与进制不匹配时,会停止读取字符串,并将已取得的字符串进行输出

参考

《C++ Primer》

《C++标准库》

http://www.cplusplus.com/reference/string/

欢迎大家评论交流,作者水平有限,如有错误,欢迎指出

C++知识点21——使用C++标准库(再谈string——string的搜索和数值转化)相关推荐

  1. C++知识点19——使用C++标准库(再谈string——string的初始化、赋值、添加、删除、访问)

    1.string的构造函数 string();//默认构造函数,默认初始化时调用 string (const string& str);//拷贝构造函数,拷贝初始化时调用 string (si ...

  2. C++知识点28——使用C++标准库(再谈迭代器)

    一.迭代器的种类 C++中的容器以及泛型算法会大量的使用迭代器 目前已经出现的迭代器有一下几种 1.输出迭代器 (OutputIterator) 输出迭代器类似于输出流(只能向输出流中写入数据),只能 ...

  3. C++知识点26——使用C++标准库(常用的泛型算法1)

    C++中实现了很多的泛型算法,大约100多个,使用前要添加#include<algorithm> 下面介绍的基本可以满足绝大部分需求,其他的用到再查 一.计数算法 1.count temp ...

  4. C++知识点32——使用C++标准库(关联容器set和multiset的初始化,赋值,查找,添加,删除与迭代器失效)

    关联容器map和multimap已经在博客https://blog.csdn.net/Master_Cui/article/details/108690877和https://blog.csdn.ne ...

  5. C++知识点30——使用C++标准库(关联容器map及其初始化,赋值,查找,添加,删除与迭代器失效)

    一.关联容器简介 关于顺序容器和关联容器的区别已经在博客https://blog.csdn.net/Master_Cui/article/details/107427911中提过 C++标准库中的关联 ...

  6. C++知识点23——使用C++标准库(顺序容器list的其他操作)

    除了博客https://blog.csdn.net/Master_Cui/article/details/107751785中介绍的操作,list还有一些其他的操作 1.reverse void re ...

  7. C++知识点33——使用C++标准库(无序关联容器unordered_(multi)map,unordered_(multi)set)

    C++中,无序关联容器一共有4个,unordered_map,unordered_set,unordered_multimap,unordered_multiset 这四个和有序关联容器最大的区别就是 ...

  8. C++知识点31——使用C++标准库(关联容器multimap及其初始化,赋值,查找,添加,删除与迭代器失效)

    关于关联容器map已经在博客https://blog.csdn.net/Master_Cui/article/details/108690877中介绍完了 multimap和map非常类似,容器中的元 ...

  9. C++知识点29——使用C++标准库(迭代器适配器)

    在上一篇文章https://blog.csdn.net/Master_Cui/article/details/108512730谈到的迭代器是基本的五种类型的迭代器 但是随着C++标准库的扩展,又实现 ...

最新文章

  1. Eclipse 设置SVN忽略文件
  2. ASP.NET高并发解决方案
  3. html原生上传,一个基于HTML5及原生JS的文件上传组件--JohnUploader
  4. 【C语言进阶深度学习记录】二十三 数组的本质分析
  5. C++primer 第 3 章 字符串、向量和数组 3 . 3 标准库类型vector
  6. [Hyper-V]使用操作系统模板创建新的虚拟机
  7. Microsoft Office Visio 2007
  8. Proguard打包混淆报错:can't find superclass or interface
  9. 安全帽检测/人脸识别系统国标GB28181协议云服务EasyGBS搭建智慧工地 助力智慧建筑生态圈
  10. Python-illegal multibyte sequence
  11. Linux禅道安装步骤以及测试初认知
  12. 三款主流数码绘画软件调研分析
  13. CAD云线怎么画?CAD云线绘制技巧
  14. 云服务器怎么装安卓系统,云服务器怎样装安卓系统
  15. python 检测文件编码_[常用] 在Python中检测网页编码
  16. mysql在视图中增加新数据_怎么向Mysql视图中增加新数据
  17. windows远程android传输文件,电脑(Linux/Windows)使用SSH远程登录安卓(Android)手机实现无线传输和管理文件(图文详解)-Go语言中文社区...
  18. cn_windows_7_ultimate_with_sp1_x64_dvd_u_677408.iso
  19. 案例:同程凤凰缓存系统基于Redis的设计与实践。
  20. 搜索系统—搜索引擎的原理,架构与细节

热门文章

  1. Oracle开发常用函数与存储过程
  2. 最怕的是,你永远也忘不掉 BY顾明烟
  3. LINUX DNS服务的配置(一)
  4. 我眼里的Exchange 2010 之:1—DAG
  5. 基因组重复序列检测:RepeatMasker
  6. CSS Grid 网格布局全解析
  7. pyinstaller深入使用,打包指定模块,打包静态文件
  8. nyoj n-1位数
  9. Windows UWP开发系列 – 3D变换
  10. 用createinstallmedia创建可恢复的OSX安装DMG