STL中string的搜索操作
来源:互联网 发布:淘宝退货卖家没收到货 编辑:程序博客网 时间:2024/06/06 07:10
string类提供了6个不同的搜索函数。每个函数有4个重载版本。每个搜索操作都返回一个string::size_type值,表示匹配发生位置的值。如果搜索失败,则返回一个string::npos的static 成员。标准库将npos定义为一个const string::size_type 类型,并初始化为值-1。
(1)find()函数
string name("AnnaBelle"); auto pos1 = name.find("Anna");// pos1 = 0
搜索是大小写敏感的。
(2) find_first_of() 和 find_first_not_of()函数
string numbens("0123456789");string name("r2d2");auto pos2 = name.find_first_of(numbers);// pos2 = 1,即name中的第一个数字string dept("03714p3");auto pos3 = dept.find_first_not_of(numbers);//pos3 = 5,即字符 'p'的下标
(3) find_last_of()
当然,也可以指定搜索的开始位置、
string::size_type pos = 0;//每步循环查找name 中的下一个数while((pos = name.find_first_of(numbers,pos) != string::npos){ cout<<"found number at index: "<< pos<<" element is " << name[pos] <<endl; ++pos;//移动到下一个字符}
(4)逆向搜索 rfind() 从有往左搜索
0 0
- STL中string的搜索操作
- C++:STL容器中,string操作
- STL的string的基本操作
- STL中string的源码解读
- C++中string的用法STL
- STL中string的源码解读
- stl中string格式化的方法
- c++ STL中string的使用方法
- STL中string的基本用法范例
- stl 中std::string 的方法
- STL中string类的用法总结
- STL中string类的使用
- STL中string与wstring的转换
- 关于STL中string的拷贝问题
- STL中string的使用 c++
- C++中STL对string进行trim,split,replace操作
- string搜索操作
- C++ STL string 基本操作
- busybox init进程分析
- Retrofit2 完全解析 探索与okhttp之间的关系(一)
- Android studio添加apt
- Android Handler
- Spring Boot Controller
- STL中string的搜索操作
- 打印出系统所有字体
- Retrofit2 源码解析
- spark的任务执行流程解析
- 被攻陷的数字签名:木马作者冒用知名网络公司签名
- 《Windows核心编程》之“SEH”
- Echarts-echart和springMVC实现堆栈图(读取JSON文件数据)
- WPF中的模板(五)- 使用Style
- 打开xampp数据库是超时问题