STL 之find,find_if,find_end,find_first_of

来源:互联网 发布:银行网络宣传方法 编辑:程序博客网 时间:2024/05/18 17:58

作用:用来在一个指定的区间中查找元素。


1,find, find_if

原型:

  1. #include <algorithm>  
  2. template <class inputItr,class size,class Type>  
  3. inputItr find(inputItr first, inputItr last, const Type& searchValue);  
  4.   
  5. template <class inputItr, class unaryPredicate>  
  6. inputItr find_if(inputItr first, inputItr last, UnaryPredicate op);  

示例代码:

  1. #include <iostream>  
  2. #include <list>  
  3.   
  4. #include <string>  
  5. #include <numeric>  
  6. #include <iterator>  
  7. #include <vector>  
  8. #include <functional>  
  9.   
  10. #include <algorithm>  
  11.   
  12. using namespace std;  
  13.   
  14. int main() {  
  15.     char cList[10] = {'a','i','C','d','e','f','o','H','u','j'};  
  16.     vector<char> charList(cList,cList+10);  
  17.   
  18.     ostream_iterator<char> screen(cout, " ");  
  19.     cout << "charList:" << endl;  
  20.     copy(charList.begin(),charList.end(),screen);  
  21.     cout << endl;  
  22.   
  23.     // 定义迭代器  
  24.     vector<char>::iterator position;  
  25.     position = find(charList.begin(),charList.end(),'d');  
  26.   
  27.     if ( position != charList.end())   
  28.     {  
  29.         cout << "position = " << (position - charList.begin()) << endl;   
  30.     } else {  
  31.         cout << "the element is not in the list" << endl;  
  32.     }  
  33.   
  34.     position = find_if(charList.begin(),charList.end(),isupper);  
  35.     if ( position != charList.end())   
  36.     {  
  37.         cout << "position = " << (position - charList.begin()) << endl;   
  38.     } else {  
  39.         cout << "the element is not in the list" << endl;  
  40.     }  
  41.   
  42.     return 0;  
  43. }  

运行结果:

charList:
a i C d e f o H u j
position = 3
position = 2


2,find_end,find_first_of

声明:

  1. #include <algorithm>  
  2. template <class forwardItr1,class forwardItr2>  
  3. forwardItr1 find_end(forwardItr1 first1, forwardItr1 last1,forwardItr2 first2,forwardItr2 last2);  
  4. template <class forwardItr1,class forwardItr2,class binaryPredicate>  
  5. forwardItr1 find_end(forwardItr1 first1, forwardItr1 last1,forwardItr2 first2,forwardItr2 last2,binaryPredicate op);  
  6.   
  7. template <class forwardItr1,class forwardItr2>  
  8. forwardItr1 find_first_of(forwardItr1 first1, forwardItr1 last1,forwardItr2 first2,forwardItr2 last2);  
  9. template <class forwardItr1,class forwardItr2,class binaryPredicate>  
  10. forwardItr1 find_first_of(forwardItr1 first1, forwardItr1 last1,forwardItr2 first2,forwardItr2 last2,binaryPredicate op);  

示例代码:

  1. #include <iostream>  
  2. #include <list>  
  3.   
  4. #include <string>  
  5. #include <numeric>  
  6. #include <iterator>  
  7. #include <vector>  
  8. #include <functional>  
  9.   
  10. #include <algorithm>  
  11.   
  12. using namespace std;  
  13.   
  14. int main() {  
  15.     int list1[10] = {12,34,56,21,34,78,34,56,12,25};  
  16.     int list2[2] = {34,56};  
  17.     int list3[3] = {56,21,35};  
  18.     int list4[5] = {33,48,21,34,73};  
  19.   
  20.     vector<int> vecList1(list1,list1+10);   
  21.     vector<int> vecList2(list2,list2+2);   
  22.     vector<int> vecList3(list3,list3+3);   
  23.     vector<int> vecList4(list4,list4+5);   
  24.   
  25.     vector<int>::iterator localtion;  
  26.     ostream_iterator<int> screen(cout, " ");  
  27.   
  28.     cout << "List1" << endl;  
  29.     copy(list1,list1+10,screen);  
  30.     cout << endl;  
  31.   
  32.     cout << "List2" << endl;  
  33.     copy(list2,list2+2,screen);  
  34.     cout << endl;  
  35.   
  36.     // find_end 查找最后一个匹配  
  37.     // 在vecList1 中查找vecList2  
  38.     localtion = find_end(vecList1.begin(),vecList1.end(),vecList2.begin(),vecList2.end());  
  39.     if (localtion != vecList1.end())  
  40.     {  
  41.         cout << "position = " << (localtion - vecList1.begin()) << endl;  
  42.     } else {  
  43.         cout << "vecList2 is not in list1" << endl;  
  44.     }  
  45.   
  46.     cout << "List3:" << endl;  
  47.     copy(vecList3.begin(),vecList3.end(),screen);  
  48.     cout << endl;  
  49.     localtion = find_end(vecList1.begin(),vecList1.end(),vecList3.begin(),vecList3.end());  
  50.     if (localtion != vecList1.end())  
  51.     {  
  52.         cout << "position = " << (localtion - vecList1.begin()) << endl;  
  53.     } else {  
  54.         cout << "vecList3 is not in list1" << endl;  
  55.     }  
  56.   
  57.     cout << "List4:" << endl;  
  58.     copy(vecList4.begin(),vecList4.end(),screen);  
  59.     cout << endl;  
  60.     // find_first_of 首次出现的位置  
  61.     // 可以检查两个容器中元素是否相互包含  
  62.     localtion = find_first_of(vecList1.begin(),vecList1.end(),vecList4.begin(),vecList4.end());  
  63.     if (localtion != vecList1.end())  
  64.     {  
  65.         cout << "position = " << (localtion - vecList1.begin()) << endl;  
  66.     } else {  
  67.         cout << "No element of List4 is in list1" << endl;  
  68.     }  
  69.     return 0;  
  70. }  

运行结果:

List1
12 34 56 21 34 78 34 56 12 25
List2
34 56
position = 6
List3:
56 21 35
vecList3 is not in list1
List4:
33 48 21 34 73
position = 1

0 0
原创粉丝点击