STL中泛型算法find_if()与sort()的应用举例及详解
2013-08-09 21:20
513 查看
请认真看下原型: //find_if()的原型: template <class InputIterator, class Predicate> InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred ); //sort()的原型: template <class RandomAccessIterator> void sort ( RandomAccessIterator first, RandomAccessIterator last ); template <class RandomAccessIterator, class Compare> void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp ); //find_if()的原型: template <class InputIterator, class Predicate> InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred ); //sort()的原型: template <class RandomAccessIterator> void sort ( RandomAccessIterator first, RandomAccessIterator last ); template <class RandomAccessIterator, class Compare> void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp ); 经常可以看到这种用法: [cpp] view plaincopyprint? // sort algorithm example #include <iostream> #include <algorithm> #include <vector> using namespace std; bool myfunction (int i,int j) { return (i<j); } struct myclass { bool operator() (int i,int j) { return (i<j);} } myobject; int main () { int myints[] = {32,71,12,45,26,80,53,33}; vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33 vector<int>::iterator it; // using default comparison (operator <): sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33 // using function as comp sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80) // using object as comp sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80) // print out content: cout << "myvector contains:"; for (it=myvector.begin(); it!=myvector.end(); ++it) cout << " " << *it; cout << endl; return 0; } // sort algorithm example #include <iostream> #include <algorithm> #include <vector> using namespace std; bool myfunction (int i,int j) { return (i<j); } struct myclass { bool operator() (int i,int j) { return (i<j);} } myobject; int main () { int myints[] = {32,71,12,45,26,80,53,33}; vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33 vector<int>::iterator it; // using default comparison (operator <): sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33 // using function as comp sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80) // using object as comp sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80) // print out content: cout << "myvector contains:"; for (it=myvector.begin(); it!=myvector.end(); ++it) cout << " " << *it; cout << endl; return 0; } 17行比较好理解,19行怎么解释呢?如果想在比较函数中加入参数又应如何解决呢? 请看下面这个例子,在C++中class与struct在这方面的应用没有太大差别,我这里用struct来实现Predicate pred与Compare comp。 [cpp] view plaincopyprint? //! /brief 检索得分映射结构体 struct score_doc { double score; string doc_id; score_doc(string doc = "", double score = 0.0):doc_id(doc), score(score){} }; typedef vector<struct score_doc *> SCORE_VCT; //! /brief 用于比较score_doc结构体实例 struct score_doc_compare { //! 用于排序算法 bool operator ()(const struct score_doc *sd1, const struct score_doc *sd2) const { return sd1->score > sd2->score; } //! 用于查找算法 bool operator ()(const struct score_doc *sd) const { return sd->doc_id == m_doc_id; } //! 用于排序算法 score_doc_compare(){} //! 用于查找算法 score_doc_compare(string doc_id):m_doc_id(doc_id){} private: string m_doc_id; }; //! 查找用法 it_vct = find_if(r_rslt_ptr->score_doc_vct.begin(),r_rslt_ptr->score_doc_vct.end(), score_doc_compare("文档1")); if(r_rslt_ptr->score_doc_vct.end() != it_vct) { cout << "存在文档1" << endl; } //! 排序用法 sort(r_rslt_ptr->score_doc_vct.begin(),r_rslt_ptr->score_doc_vct.end(), score_doc_compare()); //! /brief 检索得分映射结构体 struct score_doc { double score; string doc_id; score_doc(string doc = "", double score = 0.0):doc_id(doc), score(score){} }; typedef vector<struct score_doc *> SCORE_VCT; //! /brief 用于比较score_doc结构体实例 struct score_doc_compare { //! 用于排序算法 bool operator ()(const struct score_doc *sd1, const struct score_doc *sd2) const { return sd1->score > sd2->score; } //! 用于查找算法 bool operator ()(const struct score_doc *sd) const { return sd->doc_id == m_doc_id; } //! 用于排序算法 score_doc_compare(){} //! 用于查找算法 score_doc_compare(string doc_id):m_doc_id(doc_id){} private: string m_doc_id; }; //! 查找用法 it_vct = find_if(r_rslt_ptr->score_doc_vct.begin(),r_rslt_ptr->score_doc_vct.end(), score_doc_compare("文档1")); if(r_rslt_ptr->score_doc_vct.end() != it_vct) { cout << "存在文档1" << endl; } //! 排序用法 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了。
相关文章推荐
- STL中泛型算法find_if()与sort()的应用举例及详解
- STL中泛型算法find_if()与sort()的应用举例及详解
- STL算法find,find_if,find_if_not,sort,fill,for_each,count,adjacent_find,random_shuffle,prev_permutation
- STL find_if应用举例
- 使用STL通用算法find_if()在list中搜索对象
- python的排序函数sort,sorted在列表排序和字典排序中的应用详解和举例
- C++语法基础--泛型算法(generic algorithm)--find(),find_if()
- 使用STL中的泛型算法sort
- STL find_if应用及其他
- STL 查找vector容器中的指定对象:find()与find_if()算法
- STL_算法_查找算法(find、find_if)
- STL 算法Find_if_not+匿名函数
- STL中vector查找算法find()和find_if()深入比较
- 【每日一练】(第3期)关于STL的泛型算法count_if()
- STL_Algorithm6--find, find_if, sort, binary_search
- python的排序函数sort,sorted在列表排序和字典排序中的应用详解和举例
- 实战c++中的vector系列--vector应用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)
- STL中list链表的sort算法详解
- 实战c++中的vector系列--vector应用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)
- STL_算法_查找算法(find、find_if)