
find用法
-郑州七十三中
2023年2月23日发(作者:老促会)C++STL里提供了很多字符串操作的函数,下面的字符串查找方面的部分函数
用法简介。
1、find()
查找第一次出现的目标字符串:
/*
*Author:mybestwishes
*CreatedTime:2011/4/915:56:44
*FileName:
*/
#include
#include
usingnamespacestd;
intmain(){
strings1="abcdef";
strings2="de";
intans=(s2);//在s1中查找子串s2
cout<
system("pause");
}
说明:如果查找成功则输出查找到的第一个位置,否则返回-1;
查找从指定位置开始的第一次出现的目标字符串:
/*
*Author:mybestwishes
*CreatedTime:2011/4/915:56:44
*FileName:
*/
#include
#include
usingnamespacestd;
intmain(){
strings1="adedef";
strings2="de";
intans=(s2,2);//从s1的第二个字符开始查找子串s2
cout<
system("pause");
}
2、find_first_of()
查找子串中的某个字符最先出现的位置。find_first_of()不是全匹配,而
find()是全匹配
/*
*Author:mybestwishes
*CreatedTime:2011/4/915:56:44
*FileName:
*/
#include
#include
usingnamespacestd;
intmain(){
strings1="adedef";
strings2="dek";
intans=_first_of(s2);//从s1的第二个字符开始查找
子串s2
cout<
system("pause");
}
其中find_first_of()也可以约定初始查找的位
置:_first_of(s2,2);
3、find_last_of()
这个函数与find_first_of()功能差不多,只不过find_first_of()是从字符
串的前面往后面搜索,而find_last_of()是从字符串的后面往前面搜索。可以
自行测试一下。
4、rfind()
反向查找字符串,即找到最后一个与子串匹配的位置。
5、find_first_not_of()
找到第一个不与子串的位置。