find函数完成最简单的搜索
代码
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
string name("AnnaBelle");
auto pos1 = name.find("Anna");
cout << pos1 << endl; //找到一个与Anna匹配的下标 pos1 == 0
string lowercase("annabelle");
pos1 = lowercase.find("Anna");
cout << pos1 << endl; //没有找到要区分大小写返回npos
}
find_first_of查找与给定字符串中任何一个字符匹配的位置
find_first_not_of查找第一个不在参数中的字符
代码
#include<iostream>
#include<string>
#include<cctype>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
string number("0123456789"), name("r2d2");
auto pos = name.find_first_of(number);
cout << pos << endl; //pos == 1返回name中2的下标
string dept("03714p3");
pos = dept.find_first_not_of(number);
cout << pos << endl; //pos == 5返回dept中p的下标
}