6.30 - 每日面试题
1、find的用法
1)、查找第一次出现的目标字符串:
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
string s1 = "abcdef";
string s2 = "de";
int ans = s1.find(s2);
cout << ans << endl;
system("pause");
return 0;
}
//输出结果为:3
//如果查找失败返回-1;
2)、查找从指定位置开始的第一次出现的目标字符串
int ans = s1.find(s2, 2) ; //从S1的第二个字符开始查找子串S2
//与find输出位置是相同的
3)、find_first_of() : 查找子串中的某个字符最先出现的位置。find_first_of()不是全匹配,而find()是全匹配,其中find_first_of()也可以约定初始查找的位置:s1.find_first_of(s2, 2) ;
#include<iostream>
using namespace std;
int main(){
string s1 = "adedef";
string s2 = "ek";
int ans = s1.find_first_of(s2);
cout << ans << endl;
system("pause");
return 0;
//输出值为2
}
3.find_last_of() :这个函数与find_first_of()功能差不多,只不过find_first_of()是从字符串的前面往后 面搜索,而find_last_of()是从字符串的后面往前面搜索。
4.rfind() :反向查找字符串,即找到最后一个与子串匹配的位置
5.find_first_not_of() :找到第一个不与子串匹配的位置
C++ find函数
2、C/C++中的continue、break、return的区别
C++中break,continue,return用法