cb31a_c++_STL_算法_查找算法_(4)find_first_of

cb31a_c++_STL_算法_查找算法_(4)find_first_of
find_first_of(b,e,sb,se),sb,second begin, se,second end();
find_first_of(b,e,sb,se,bp),bp--谓词,就是一个函数,或者函数对象,返回一个bool

使用逆向迭代器,实现string类的rfind.找最后一个。reverse_find=rfind
没有find_last_of算法,string类的成员函数有find_last_of

find()
find_if()
search_in()
serarch()
find_end()
find_first_of()
adjacent_find()
txwtech@163.com

 1 /*cb31a_c++_STL_算法_查找算法_(4)find_first_of
 2 find_first_of(b,e,sb,se),sb,second begin, se,second end();
 3 find_first_of(b,e,sb,se,bp),bp--谓词,就是一个函数,或者函数对象,返回一个bool
 4 
 5 使用逆向迭代器,实现string类的rfind.找最后一个。reverse_find=rfind
 6 没有find_last_of算法,string类的成员函数有find_last_of
 7 
 8 find()
 9 find_if()
10 search_in()
11 serarch()
12 find_end()
13 find_first_of()
14 adjacent_find()
15 txwtech@163.com
16 */
17 
18 #include <iostream>
19 #include <vector>
20 #include <list>
21 #include <algorithm>
22 #include <string>
23 
24 using namespace std;
25 
26 int main()
27 {
28     vector<int> ivec;
29     list<int> searchList;
30     for (int i = 1; i <= 11; ++i)
31     {
32         ivec.push_back(i);
33     }
34     for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter)
35         cout << *iter << ' ';
36     cout << endl;
37     searchList.push_back(3);
38     searchList.push_back(6);
39     searchList.push_back(9);
40 
41     cout << "ivec里面找,3,6,9" << endl;
42     vector<int>::iterator pos;
43     pos=find_first_of(ivec.begin(), ivec.end(), searchList.begin(), searchList.end());
44     if (pos != ivec.end())
45         cout << "找到了,位置是:" << distance(ivec.begin(), pos) + 1 << endl;
46     else
47         cout << "没找到" << endl;
48 
49     vector<int>::reverse_iterator rpos;
50     // rpos.base(),转换成正向迭代器,就不需要加1了
51     rpos= find_first_of(ivec.rbegin(), ivec.rend(), searchList.begin(), searchList.end());
52     if (rpos != ivec.rend())
53         cout << "逆向迭代器找到了,位置是:" << distance(ivec.begin(), rpos.base()) << endl;
54     else
55         cout << "没找到" << endl;
56 
57     string numerics("0123456789");
58     string name("ra82d3k");
59     
60     cout << "name里面找numerics的内容" << endl;
61     string::size_type posn= name.find_first_of(numerics);//顺序查找
62     if (posn != string::npos)
63         cout << "posn不等于npos,npos表示结束位置,已经扎到了,下标:" << posn << endl;
64     else
65         cout << "没找到" << endl;
66 
67     posn = name.find_last_of(numerics);//逆向查找
68     if (posn != string::npos)
69         cout << "逆向查找,最后一位开始找,已经扎到了,下标:" << posn << endl;
70     else
71         cout << "没找到" << endl;
72 
73 
74     return 0;
75 }
欢迎讨论,相互学习。 txwtech@163.com
原文地址:https://www.cnblogs.com/txwtech/p/12336972.html