find_first_of(b,e,sb,se)
find_first_of(b,e,sb,se,bp)
使用逆向迭代器 没有find_last_of算法
STL 查找算法
find()
find_if()
search_n()
search()
find_end()
find_first_of()
adjacent_find()
string 查找函数和STL查找算法的比较
string函数 STL算法
find() find()
rfind() find() + 逆向迭代器
find() search()
rfind() find_end()
find_first_of() find_first_of()
find_last_of() find_first_of() + 逆向迭代器
find() 和rfind()是一对,search()和find_end()是一对
#include<iostream> #include<vector> #include<list> #include<string> #include<algorithm> using namespace std; int main() { vector<int> ivec; list<int> searchList; for (int i = 1; i <= 11; i++) ivec.push_back(i); for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); iter++) { cout << *iter; } cout << endl; searchList.push_back(3); searchList.push_back(6); searchList.push_back(9); // 找任意一个 vector<int>::iterator pos; pos = find_first_of(ivec.begin(), ivec.end(), searchList.begin(), searchList.end()); if (pos!=ivec.end()) { cout << "找到了!位置:" << distance(ivec.begin(), pos) + 1 << endl; } else cout << "没找到!"; //从后面找 vector<int>::reverse_iterator rpos; rpos = find_first_of(ivec.rbegin(), ivec.rend(), searchList.begin(), searchList.end()); cout << "找到的位置:" << distance(ivec.begin(), rpos.base()) << endl; // string numerics("0123456789"); string name("ra8d3k"); // 找任何一个 string::size_type p = name.find_first_of(numerics); if (p != string::npos) cout << "找到了,下标:" << p << endl; else cout << "没找到!" << endl; //从后面开始找 p = name.find_last_of(numerics); if (p != string::npos) cout << "找到了,下标:" << p << endl; else cout << "没找到!" << endl; // system("pause"); return 0; }
时间: 2024-10-12 11:31:24