STL 之find系列函数
1.find()
find()主要用于查找符合的字符串,返回是一个索引值,查找可以面向char定义的数组,也可以是string类型的字符串。
#include<iostream>
using namespace std;
int main() {
string str = "abcdefg";
cout<<str.find('c');
}
运行结果为2。find()也可以这样写,find(‘c’,1),表示的是从第一个开始查找,如果写为find(‘c’,3),表示从第三个开始查找,第三个是d,结果肯定查找不到,返回值则是一串很诡异的数字(应该等于-1)。find的查找对象必须和内容完全一致才会输出索引值。
2.rfind()
它与find一样,是完全查找,查找内容与对象必须完全匹配,而rfind()是反向。
3.find_first_of()
find_first_of()属于不完全查找,不必与查找内容完全一致,格式与find()类似。
#include<iostream>
#include<cstdio>
using namespace std;
int main() {
string str = "abcdefghijklmn";
string word = "ho";
cout << str.find_first_of(word);
}
输出结果就是h的索引值。
4.find_last_of()
它与find_first_of()类似,唯一不同的是它是反向查找,当然也是不完全查找。
5.find_first_not_of()
属于不完全查找,查找的是与内容不匹配的地方。
#include<iostream>
#include<cstdio>
using namespace std;
int main() {
string str = "abcdefghijklmn";
string word = "abcde";
cout << str.find_first_not_of(word);
}
这个的运行结果就是5