C++的STL之search()与find_end()

1、search()
search(ideq.begin(),ideq.end(), ilist.begin(), ilist.end());
2、find_end()
find_end(ideq.begin(), ideq.end(),ilist.begin(),ilist.end());
也可以用谓词:
bool checkEven(int elem, bool even)
{
if(even)
return elem % 2 == 0;
else
return elem % 2 == 1;
}
bool checkEvenArgs[4] = {false, true, true, false};
pos = search(ivec.begin(), ivec.end(),checkEvenArgs,checkEvenArgs+4,checkEven);//查找连续4个都是true的
注意:

  这两个算法是一对

  第二个算法应该叫search_end(), 但是被C++标准委员会命名为find_end()

[cpp]  view plain  copy
  1. #include<iostream>    
  2. #include<cstdio>    
  3. #include<cstring>    
  4. #include<vector>    
  5. #include<list>    
  6. #include<deque>    
  7. #include<algorithm>    
  8. using namespace std;    
  9. /*************************************************************************************  
  10. std::search       从左往右找子区间    所有容器适用                       algorithm  
  11. --------------------------------------------------------------------------------------  
  12. template <class ForwardIterator1, class ForwardIterator2>  
  13. ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1,  
  14.                           ForwardIterator2 first2, ForwardIterator2 last2 );  
  15.   
  16. template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>  
  17. ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1,  
  18.                           ForwardIterator2 first2, ForwardIterator2 last2.  
  19.                           BinaryPredicate pred );  
  20.   
  21. eg:  
  22. template<class ForwardIterator1, class ForwardIterator2>  
  23. ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1,  
  24.                           ForwardIterator2 first2, ForwardIterator2 last2)  
  25.  
  26.     if (first2==last2) return first1;  // specified in C++11  
  27.   
  28.     while (first1!=last1)  
  29.     {  
  30.         ForwardIterator1 it1 = first1;  
  31.         ForwardIterator2 it2 = first2;  
  32.         while (*it1==*it2)      // or: while (pred(*it1,*it2)) for the pred version  
  33.         {  
  34.             ++it1;  
  35.             ++it2;  
  36.             if (it2==last2) return first1;  
  37.             if (it1==last1) return last1;  
  38.         }  
  39.         ++first1;  
  40.     }  
  41.     return last1;  
  42.  
  43. **************************************************************************************/    
  44.     
  45. /*************************************************************************************  
  46. std::find_end       从右往左找子区间     所有容器适用                     algorithm  
  47. --------------------------------------------------------------------------------------  
  48. template <class ForwardIterator1, class ForwardIterator2>  
  49. ForwardIterator1 find_end ( ForwardIterator1 first1, ForwardIterator1 last1,  
  50.                             ForwardIterator2 first2, ForwardIterator2 last2 );  
  51.   
  52. template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>  
  53. ForwardIterator1 find_end ( ForwardIterator1 first1, ForwardIterator1 last1,  
  54.                             ForwardIterator2 first2, ForwardIterator2 last2,  
  55.                             BinaryPredicate pred );  
  56.   
  57. eg:  
  58. template<class ForwardIterator1, class ForwardIterator2>  
  59. ForwardIterator1 find_end ( ForwardIterator1 first1, ForwardIterator1 last1,  
  60.                             ForwardIterator2 first2, ForwardIterator2 last2)  
  61.  
  62.     if (first2==last2) return last1;  // specified in C++11  
  63.   
  64.     ForwardIterator1 ret = last1;  
  65.   
  66.     while (first1!=last1)  
  67.     {  
  68.         ForwardIterator1 it1 = first1;  
  69.         ForwardIterator2 it2 = first2;  
  70.         while (*it1==*it2)      // or: while (pred(*it1,*it2)) for the pred version  
  71.         {  
  72.             ++it1;  
  73.             ++it2;  
  74.             if (it2==last2)  
  75.             {  
  76.                 ret=first1;  
  77.                 break;  
  78.             }  
  79.             if (it1==last1) return ret;  
  80.         }  
  81.         ++first1;  
  82.     }  
  83.     return ret;  
  84.  
  85. **************************************************************************************/    
  86.     
  87. bool mypredicate (int i, int j)    
  88. {    
  89.     return (i==j);    
  90. }    
  91. bool myfunction (int i, int j)    
  92. {    
  93.     return (i==j);    
  94. }    
  95. bool check(int elem,bool bo)//二元谓词    
  96. {    
  97.     if(bo) return !(elem&1);    
  98.     else   return  elem&1;    
  99. }    
  100.     
  101. int main ()    
  102. {    
  103.     vector<int> myvector;    
  104.     vector<int>::iterator it;    
  105.     
  106.     // set some values:        myvector: 10 20 30 40 50 60 70 80 90    
  107.     for (int i=1; i<10; i++) myvector.push_back(i*10);    
  108.     
  109.     
  110.     // using default comparison:    
  111.     int match1[] = {40,50,60,70};    
  112.     
  113.     it = search (myvector.begin(), myvector.end(), match1, match1+4);    
  114.     
  115.     if (it!=myvector.end())    
  116.         cout << "match1 found at position " << int(it-myvector.begin()) << endl;    
  117.     else    
  118.         cout << "match1 not found" << endl;    
  119.     
  120.     // using predicate comparison:    
  121.     int match2[] = {20,30,50};    
  122.     it = search (myvector.begin(), myvector.end(), match2, match2+3, mypredicate);    
  123.     
  124.     if (it!=myvector.end())    
  125.         cout << "match2 found at position " << int(it-myvector.begin()) << endl;    
  126.     else    
  127.         cout << "match2 not found" << endl;    
  128.     /**--------------------------------------find_end-----------------------------------------**/    
  129.     
  130.     int myints[] = {1,2,3,4,5,1,2,3,4,5,1};    
  131.     deque<int> mydeque (myints,myints+11);    
  132.     deque<int>::iterator itd;    
  133.     
  134.     int match3[] = {1,2,3};    
  135.     
  136.     // using default comparison:    
  137.     itd = find_end (mydeque.begin(), mydeque.end(), match3, match3+3);    
  138.     
  139.     if (itd!=mydeque.end())    
  140.         cout << "match1 last found at position " << int(itd-mydeque.begin()) << endl;    
  141.     
  142.     int match4[] = {4,5,1};    
  143.     
  144.     // using predicate comparison:    
  145.     itd = find_end (mydeque.begin(), mydeque.end(), match4, match4+3, myfunction);    
  146.     
  147.     if (itd!=mydeque.end())    
  148.         cout << "match2 last found at position " << int(itd-mydeque.begin()) << endl;    
  149.     
  150.     /**--------------------------拓展找:偶数奇数奇数------------------------------**/    
  151.     
  152.     cout<<"\n1  2  3  4  5  1  2  3  4  5  1"<<endl;    
  153.     vector<int> vec(myints,myints+11);    
  154.     bool checkEven[3]={true,false,false};    
  155.     //search    
  156.     it=search(vec.begin(),vec.end(),checkEven,checkEven+3,check);    
  157.     //find_end    
  158.     itd=find_end(mydeque.begin(),mydeque.end(),checkEven,checkEven+3,check);    
  159.     
  160.     if (it!=vec.end())    
  161.         cout << " even odd odd found at position " << int(it-vec.begin()) << endl;    
  162.     else    
  163.         cout << "not found" << endl;    
  164.     
  165.     if (itd!=mydeque.end())    
  166.         cout << " even odd odd found at position " << int(itd-mydeque.begin()) << endl;    
  167.     else    
  168.         cout << "not found" << endl;    
  169.     return 0;    
  170. }    
  171.     
  172. /******  
  173. Output:  
  174.     match1 found at position 3  
  175.     match2 not found  
  176.     match1 last found at position 5  
  177.     match2 last found at position 3  
  178.       
  179.     1  2  3  4  5  1  2  3  4  5  1  
  180.      even odd odd found at position 3  
  181.      even odd odd found at position 8  
  182. ******/    

猜你喜欢

转载自blog.csdn.net/jisuanji198509/article/details/80743851