STL中泛型算法find_if()与sort()的应用举例及详解

本文通过示例讲解了STL中find_if()与sort()的使用方法,并详细介绍了如何自定义比较函数与谓词,包括如何为这些算法提供额外参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

         STL中泛型算法find_if()与sort()的应用举例及详解

分类: C/C++ 418人阅读 评论(2) 收藏 举报

STL中泛型算法find_if()与sort()的应用举例及详解

 

请认真看下原型:

  1. //find_if()的原型:  
  2. template <class InputIterator, class Predicate>  
  3.    InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred );  
  4. //sort()的原型:  
  5. template <class RandomAccessIterator>  
  6.   void sort ( RandomAccessIterator first, RandomAccessIterator last );  
  7. template <class RandomAccessIterator, class Compare>  
  8.   void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );  


经常可以看到这种用法:
  1. // sort algorithm example  
  2. #include <iostream>  
  3. #include <algorithm>  
  4. #include <vector>  
  5. using namespace std;  
  6. bool myfunction (int i,int j) { return (i<j); }  
  7. struct myclass {  
  8.   bool operator() (int i,int j) { return (i<j);}  
  9. } myobject;  
  10. int main () {  
  11.   int myints[] = {32,71,12,45,26,80,53,33};  
  12.   vector<int> myvector (myints, myints+8);               // 32 71 12 45 26 80 53 33  
  13.   vector<int>::iterator it;  
  14.   // using default comparison (operator <):  
  15.   sort (myvector.begin(), myvector.begin()+4);           //(12 32 45 71)26 80 53 33  
  16.   // using function as comp  
  17.   sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)  
  18.   // using object as comp  
  19.   sort (myvector.begin(), myvector.end(), myobject);     //(12 26 32 33 45 53 71 80)  
  20.   // print out content:  
  21.   cout << "myvector contains:";  
  22.   for (it=myvector.begin(); it!=myvector.end(); ++it)  
  23.     cout << " " << *it;  
  24.   cout << endl;  
  25.   return 0;  
  26. }  


17行比较好理解,19行怎么解释呢?如果想在比较函数中加入参数又应如何解决呢?

请看下面这个例子,在C++中class与struct在这方面的应用没有太大差别,我这里用struct来实现Predicate pred与Compare comp。

  1. //! /brief 检索得分映射结构体  
  2. struct score_doc  
  3. {  
  4.     double score;  
  5.     string doc_id;  
  6.     score_doc(string doc = ""double score = 0.0):doc_id(doc), score(score){}  
  7. };  
  8. typedef vector<struct score_doc *> SCORE_VCT;  
  9. //! /brief 用于比较score_doc结构体实例  
  10. struct score_doc_compare  
  11. {  
  12.     //! 用于排序算法  
  13.     bool operator ()(const struct score_doc *sd1, const struct score_doc *sd2) const  
  14.     {  
  15.         return sd1->score > sd2->score;  
  16.     }  
  17.     //! 用于查找算法  
  18.     bool operator ()(const struct score_doc *sd) const  
  19.     {  
  20.         return sd->doc_id == m_doc_id;  
  21.     }  
  22.     //! 用于排序算法  
  23.     score_doc_compare(){}  
  24.     //! 用于查找算法  
  25.     score_doc_compare(string doc_id):m_doc_id(doc_id){}  
  26.     private:  
  27.     string m_doc_id;  
  28. };  
  29. //! 查找用法  
  30. it_vct = find_if(r_rslt_ptr->score_doc_vct.begin(),r_rslt_ptr->score_doc_vct.end(), score_doc_compare("文档1"));  
  31. if(r_rslt_ptr->score_doc_vct.end() != it_vct)  
  32. {  
  33.     cout << "存在文档1" << endl;  
  34. }  
  35. //! 排序用法  
  36. sort(r_rslt_ptr->score_doc_vct.begin(),r_rslt_ptr->score_doc_vct.end(), score_doc_compare());  


由此可见find_if()调用的实际是score_doc_compare("文档1")(),同理sort()调用的是score_doc_compare()()。
在C++中,score_doc_compare("文档1")或score_doc_compare()创建了一个结构体实例,然后由find_if()或sort()调用了()操作符。
如果想加入参数,如上例中find_if的用法,只需在Compare comp中添加载入对应参数的构造函数和成员变量,然后重载()操作符时调取这个成员变量当参数就OK了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值