cb30a_c++_STL_算法_查找算法_(3)search_find_end
cb30a_c++_STL_算法_查找算法_(3)search_find_end
search()
pos = search(ideq.begin(), ideq.end(), ilist.begin(), ilist.end());
find_end(),从后面开始找
注意:
这两个算法是一对
第二个算法应该叫search_end(),但是被命名为find_end()
pos2 = search(ivec.begin(), ivec.end(),checkEvenArgs,checkEvenArgs+3, checkEven);
//ivec.begin(), ivec.end()传给第一个参数,elem.
//,checkEvenArgs,checkEvenArgs+3,传给checkEven的第二个参数,even
//bool checkEven(int elem, bool even)//二元谓词,两个return
1 /*cb30a_c++_STL_算法_查找算法_(3)search_find_end 2 search() 3 pos = search(ideq.begin(), ideq.end(), ilist.begin(), ilist.end()); 4 find_end(),从后面开始找 5 6 注意: 7 这两个算法是一对 8 第二个算法应该叫search_end(),但是被命名为find_end() 9 10 pos2 = search(ivec.begin(), ivec.end(),checkEvenArgs,checkEvenArgs+3, checkEven); 11 //ivec.begin(), ivec.end()传给第一个参数,elem. 12 //,checkEvenArgs,checkEvenArgs+3,传给checkEven的第二个参数,even 13 //bool checkEven(int elem, bool even)//二元谓词,两个return 14 */ 15 #include <iostream> 16 #include <algorithm> 17 #include <deque> 18 #include <list> 19 #include <vector> 20 21 using namespace std; 22 23 //1,2,2,4,5 24 //true, false, true 25 //false表示奇数 26 //true 偶数 27 28 bool checkEven(int elem, bool even)//二元谓词,两个return 29 { 30 if (even) 31 return elem % 2 == 0;// 32 else 33 return elem % 2 == 1;// 34 } 35 //把 true, false, true,三个传进去,连续3个都返回true,说明查找到了。 36 37 int main() 38 { 39 deque<int> ideq; 40 list<int> ilist; 41 for (int i = 1; i <= 7; ++i) 42 ideq.insert(ideq.end(), i);//插入到最后 43 for (int i = 1; i <= 7; ++i) 44 ideq.insert(ideq.end(), i);//插入到最后 45 for (deque<int>::iterator iter = ideq.begin(); iter != ideq.end(); ++iter) 46 cout << *iter << ' '; 47 cout << endl; 48 for (int i = 3; i <= 6; ++i) 49 ilist.insert(ilist.end(), i); 50 for (list<int>::iterator iter = ilist.begin(); iter != ilist.end(); ++iter) 51 cout << *iter << ' '; 52 cout << endl; 53 54 deque<int>::iterator pos; 55 pos = search(ideq.begin(), ideq.end(), ilist.begin(), ilist.end()); 56 cout << "在ideq容器里面找ilist" << endl; 57 if (pos != ideq.end()) 58 cout << "找到了位置:" << distance(ideq.begin(), pos)+1 << endl; 59 else 60 cout << "没找到" << endl; 61 ++pos; 62 pos = search(pos, ideq.end(), ilist.begin(), ilist.end()); 63 cout << "在ideq容器里面找ilist" << endl; 64 if (pos != ideq.end()) 65 cout << "找到了位置:" << distance(ideq.begin(), pos)+1 << endl; 66 else 67 cout << "没找到" << endl; 68 69 cout << "使用find_end,从后面开始查找。进行查找:" << endl; 70 pos = find_end(ideq.begin(), ideq.end(), ilist.begin(), ilist.end()); 71 72 if (pos != ideq.end()) 73 cout << "找到了位置:" << distance(ideq.begin(), pos) + 1 << endl; 74 else 75 cout << "没找到" << endl; 76 77 cout << "使用谓词查找" << endl; 78 79 vector<int> ivec; 80 bool checkEvenArgs[3] = {true,false,true}; 81 for (int i = 1; i <= 9; ++i) 82 ivec.push_back(i); 83 for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter) 84 cout << *iter << ' '; 85 cout << endl; 86 vector<int>::iterator pos2; 87 pos2 = search(ivec.begin(), ivec.end(),checkEvenArgs,checkEvenArgs+3, checkEven); 88 //ivec.begin(), ivec.end()传给第一个参数,elem. 89 //,checkEvenArgs,checkEvenArgs+3,传给checkEven的第二个参数,even 90 //bool checkEven(int elem, bool even)//二元谓词,两个return 91 92 93 //1,2,3,4,5,6,7 94 //true,false,true. 对应2,3,4。所以在第二就找到了。显示2 95 if (pos2 != ivec.end()) 96 cout << "找到了,位置是:" << distance(ivec.begin(), pos2)+1 << endl; 97 else 98 cout << "没找到" << endl; 99 return 0; 100 }
欢迎讨论,相互学习。
cdtxw@foxmail.com