STL 之search,search_n,sort,binary_search

作用:用来查找元素和元素排序


 声明:

  1. #include <algorithm>  
  2. template <class forwardItr1, class forwardItr2>  
  3. forwardItr1 search(forwardItr1 first1, forwardItr1 last1,forwardItr2 first2,forwardItr2 last2);  
  4. template <class forwardItr1, class forwardItr2,class binaryPredicate>  
  5. forwardItr1 search(forwardItr1 first1, forwardItr1 last1,forwardItr2 first2,forwardItr2 last2,binaryPredicate op);  
  6.   
  7. template <class forwardItr, class size,class Type>  
  8. forwardItr search_n(forwardItr first, forwardItr last,size count,const Type& value);  
  9. template <class forwardItr, class size,class Type,class binaryPredicate>  
  10. forwardItr search_n(forwardItr first, forwardItr last,size count,const Type& value,binaryPredicate op);  
  11.   
  12. template<class randomAccessItr>  
  13. void sort(randomAccessItr first,randomAccessItr last);  
  14. template<class randomAccessItr, class compare>  
  15. void sort(randomAccessItr first, randomAccessItr last, compare op);  
  16.   
  17. template<class forwardItr,class Type>  
  18. bool binary_search(forwardItr first,forwardItr last,const Type& searchValue);  
  19. template<class forwardItr,class Type,class compare>  
  20. bool binary_search(forwardItr first, forwardItr last, const Type& searchValue,compare 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 intList[15] = {12,34,56,34,34,  
  16.                        78,38,43,12,25,  
  17.                        34,56,62,5,49};  
  18.     vector<int> vecList(intList,intList+15);  
  19.     int list[2] = {34, 56};  
  20.     vector<int>::iterator location;  
  21.   
  22.     ostream_iterator<int> screen(cout, " ");  
  23.   
  24.     cout << "vecList:" << endl;  
  25.     copy(vecList.begin(),vecList.end(),screen);  
  26.     cout << endl;  
  27.     cout << "list:" << endl;  
  28.     copy(list,list+2,screen);  
  29.     cout << endl;  
  30.   
  31.     // search:查找一个集合是否在另一集合中  
  32.     // 与find的区别是,find是查找某一个元素  
  33.     location = search(vecList.begin(),vecList.end(),list,list + 2);  
  34.     if (location != vecList.end())  
  35.     {  
  36.         cout << "location:" << (location - vecList.begin()) << endl;  
  37.     } else {  
  38.         cout << "list is not in vecList" << endl;  
  39.     }  
  40.     // search_n :查找某个元素第n此出现的位置  
  41.     location = search_n(vecList.begin(),vecList.end(),2,34);  
  42.     if (location != vecList.end())  
  43.     {  
  44.         cout << "location:" << (location - vecList.begin()) << endl;  
  45.     } else {  
  46.         cout << "list is not in vecList" << endl;  
  47.     }  
  48.   
  49.     // sort  
  50.     sort(vecList.begin(),vecList.end());  
  51.     cout << "vecList:" << endl;  
  52.     copy(vecList.begin(),vecList.end(),screen);  
  53.     cout << endl;  
  54.   
  55.     bool found;  
  56.     // 用二分法查找:前提先排序  
  57.     found = binary_search(vecList.begin(),vecList.end(),43);  
  58.   
  59.     if (found)  
  60.     {  
  61.         cout << "43 found in vecList." << endl;  
  62.     } else {  
  63.         cout << "43 not found in vecList." << endl;  
  64.     }  
  65.   
  66.     return 0;  
  67. }  

运行结果:

vecList:
12 34 56 34 34 78 38 43 12 25 34 56 62 5 49
list:
34 56
location:1
location:3
vecList:
5 12 12 25 34 34 34 34 38 43 49 56 56 62 78
43 found in vecList.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值