cb28a_c++_STL_算法_查找算法_(1)find_find_if
cb28a_c++_STL_算法_查找算法_(1)find_find_if
find() //线性查找,比较慢。
pos1 = find(ilist.begin(), ilist.end(), 5);
find_if()
search_n()
search()
find_end()
find_first_of()
adjacent_find()
注意:
1.如果是已序区间,可以使用已序区间的查找算法(效率高)
binary_search()
includes()
lower_bound()
upper_bound()
2.关联是容器有等效的成员函数find(),set,map
3.string有等效的成员函数find(),不能使用 算法的 find(),find_if().算法的find适合所有容器使用
1 /*cb28a_c++_STL_算法_查找算法_(1)find_find_if 2 find() //线性查找,比较慢。 3 pos1 = find(ilist.begin(), ilist.end(), 5); 4 find_if() 5 search_n() 6 search() 7 find_end() 8 find_first_of() 9 adjacent_find() 10 注意: 11 1.如果是已序区间,可以使用已序区间的查找算法(效率高) 12 binary_search() 13 includes() 14 lower_bound() 15 upper_bound() 16 2.关联是容器有等效的成员函数find(),set,map 17 3.string有等效的成员函数find(),不能使用 算法的 find(),find_if().算法的find适合所有容器使用 18 */ 19 20 #include <iostream> 21 #include <algorithm> 22 #include <list> 23 24 using namespace std; 25 int main() 26 { 27 list<int> ilist; 28 for (int i = 1; i <= 9; ++i) 29 ilist.insert(ilist.end(), i); 30 for (int i = 1; i <= 9; ++i) 31 ilist.insert(ilist.end(), i); 32 for (list<int>::iterator iter = ilist.begin(); iter != ilist.end(); ++iter) 33 cout << *iter << ' '; 34 cout << endl; 35 36 list<int>::iterator pos1; 37 pos1 = find(ilist.begin(), ilist.end(), 5); 38 cout << "找到后返回迭代器" << endl; 39 list<int>::iterator pos2; 40 if (pos1 != ilist.end()) 41 { 42 pos2 = find(++pos1, ilist.end(), 5); 43 44 } 45 if (pos1 != ilist.end() && pos2 != ilist.end()) 46 { 47 cout << "显示两个5之间的数据" << endl; 48 for (list<int>::iterator iter = pos1; iter != pos2; ++iter) 49 cout << *iter << ' '; 50 cout << endl; 51 } 52 53 54 }
1 /*cb28b_c++_ 2 3 */ 4 #include <iostream> 5 #include <algorithm> 6 #include <vector> 7 #include <functional> 8 9 using namespace std; 10 11 int main() 12 { 13 vector<int> ivec; 14 vector<int>::iterator pos; 15 16 for (int i = 8; i >= 1; --i) 17 ivec.push_back(i); 18 for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter) 19 cout << "vector数据是:" << *iter << ' '; 20 cout << endl; 21 22 pos = find_if(ivec.begin(), ivec.end(), bind2nd(greater<int>(), 3)); 23 //https://www.cnblogs.com/txwtech/p/12330413.html 24 cout <<"大于3的数: "<< *pos << endl; 25 26 27 pos = find_if(ivec.begin(), ivec.end(), not1(bind2nd(modulus<int>(), 3))); 28 //modulus取模运算:找一个被3整除的数,找到后,bind2nd返回的取模的结果是0 29 //not1取反,就可以返回1 30 //https://www.cnblogs.com/txwtech/p/12330413.html 31 cout << "找一个被3整除的数: " <<*pos<< endl; 32 return 0; 33 }
1 /*cb28c_c++ 2 3 txwtech@163.com 4 */ 5 6 #include <iostream> 7 #include <set> 8 9 using namespace std; 10 11 int main() 12 { 13 set<int> iset; 14 iset.insert(43); //插入数据,自动排序。已序区间 15 iset.insert(78); 16 iset.insert(-1); 17 iset.insert(124); 18 19 for (set<int>::iterator iter = iset.begin(); iter != iset.end(); ++iter) 20 cout << *iter << ' '; 21 cout << endl; 22 set<int>::iterator pos; 23 pos = iset.find(78);//类似2分查找,速度快 24 if (pos != iset.end()) 25 cout << "找到了" << *pos << endl; 26 else 27 cout << "没找到" << endl; 28 29 30 31 return 0; 32 }
1 /*cb28d 2 */ 3 #include <iostream> 4 #include <string> 5 6 using namespace std; 7 8 int main() 9 { 10 string s("AnnaBelle"); 11 12 string::size_type pos = s.find("nna"); 13 if (pos != string::npos) 14 cout << "找到了,下标是:" << pos << endl; 15 else 16 cout << "没找到" << endl; 17 18 pos = s.find("bell"); 19 if (pos != string::npos) 20 cout << "找到了,下标是:" << pos << endl; 21 else 22 cout << "没找到" << endl; 23 24 return 0; 25 }
欢迎讨论,相互学习。
cdtxw@foxmail.com