C++中find函数用法
来源:互联网 发布:win7系统优化 编辑:程序博客网 时间:2024/06/06 09:30
C++中STL里提供了许多字符串操作的函数,下面是字符串查找方面的部分函数用法简介:
1.find()
查找第一次出现的目标字符串:
#include<iostream>
#include<csdtio>
using namespace std;
int main(){
string s1 = "abcdef";
string s2 = "de";
int ans = s1.find(s2) ; //在S1中查找子串S2
cout<<ans<<endl;
system("pause");
}
说明:如果查找成功则输出查找到的第一个位置,否则返回-1;
查找从指定位置开始的第一次出现的目标字符串:
#include<iostream>
#include<csdtio>
using namespace std;
int main(){
string s1 = "abcdef";
string s2 = "de";
int ans = s1.find(s2, 2) ; //从S1的第二个字符开始查找子串S2
cout<<ans<<endl;
system("pause");
}
2.find_first_of()
查找子串中的某个字符最先出现的位置。find_first_of()不是全匹配,而find()是全匹配
#include<iostream>
#include<csdtio>
using namespace std;
int main(){
string s1 = "adedef";
string s2 = "dek";
int ans = s1.find_first_of(s2) ; //在S1中查找子串S2
cout<<ans<<endl;
system("pause");
}
其中find_first_of()也可以约定初始查找的位置:s1.find_first_of(s2, 2) ;
3.find_last_of()
这个函数与find_first_of()功能差不多,只不过find_first_of()是从字符串的前面往后面搜索,而find_last_of()是从字符串的后面往前面搜索。
4.rfind()反向查找字符串,即找到最后一个与子串匹配的位置
5.find_first_not_of()
找到第一个不与子串匹配的位置
- c++find函数用法
- c++find函数用法
- c++find函数用法
- string中find函数用法
- Matlab中find函数用法
- MATLAB中find函数用法
- C++中find函数用法
- Octave中find函数用法
- c++string.find()函数用法整理
- c++string.find()函数用法
- matlab中find函数简单用法
- c++中find函数的用法
- matlab中find函数的用法
- Matlab中find函数的用法
- matlab中find、sub2ind函数用法
- C++: string 中find函数的用法以及string::npos的含义
- C中malloc函数用法
- C 中scanf ( ) 函数用法
- Hrbust 2171 最佳工厂【贪心+优先队列】经典题型
- 位运算,位移,窗体
- ObjectOutputStream 与 ObjectInputStream创建的先后顺序
- Java集合---ConcurrentHashMap原理分析
- Vue.js入门
- C++中find函数用法
- Eclipse Maven 更新或者导入中异常关闭无法启动
- [转] C++中字符型变量的地址输出
- hdu1847(巴什博弈)
- uva10041
- bzoj 2134: 单选错位 (概率与期望)
- Ubuntu上使用Tomcat部署web应用
- [bzoj2152]聪明可可 点分治
- rk3288 lvds TF卡修改屏参