注:博客内容均来自于对《C++标准库》侯捷,华中科技大学出版社一书的笔记。转载请注明出处。
所有例程在Red Hat Linux 3.2.2-5版本上编译运行,g++的版本是 g++ (GCC) 3.2.2 20030222。
第二个参数排序准则的型别,实际的排序准则是容器所产生的仿函数。下面一个例子来说明这种方式。
example:
<代码段>#include <iostream> #include <string> #include <set> #include <algorithm> using namespace std; class Person { public: Person(string a,string b) : strFirstname(a),strLastname(b) {} public: string firstname() const { return strFirstname; } string lastname() const { return strLastname; } private: const string strFirstname; const string strLastname; }; //仿函数实现自定义排序 class PersonSortCriterion { public : bool operator()(const Person &p1, const Person &p2) { return (p1.lastname() < p2.lastname() || (!(p2.lastname() < p1.lastname()) && p1.firstname() < p2.firstname())); } }; int main(int argc, char *argv[]) { typedef set<Person, PersonSortCriterion> PersonSet; PersonSet col1; //创建元素,并添加到容器 Person p1("Jay","Chou"); Person p2("Robin","Chou"); Person p3("Robin","Lee"); Person p4("Bob","Smith"); col1.insert(p1); col1.insert(p2); col1.insert(p3); col1.insert(p4); PersonSet::iterator pos; //输出PersonSet中的所有元素 for(pos = col1.begin(); pos != col1.end(); ++pos) { cout<<pos->firstname()<< " " << pos->lastname() << endl; } cout<<endl; return 0; }
如果执行期间才获得排序准则,而且需要用到不同的排序准则,那么使用这种方式再好不过了!
example:
#include <iostream> #include <set> #include "print.h" using namespace std; template <class T> class RuntimeCmp { public: enum cmp_mode {normal, reverse}; private: cmp_mode mode; public: RuntimeCmp(cmp_mode m = normal): mode(m) { } bool operator() (const T& t1, const T& t2) const { return mode == normal ? t1<t2 : t2<t1; } bool operator== (const RuntimeCmp& rc) { return mode == rc.mode; } }; typedef set<int, RuntimeCmp<int> > IntSet; void fill(IntSet& set) { set.insert(4); set.insert(7); set.insert(5); set.insert(1); set.insert(6); set.insert(2); set.insert(5); } int main(int argc, char *argv[]) { IntSet col1; fill(col1); PRINT_ELEMENTS(col1, "col1: "); RuntimeCmp<int> reverse_order(RuntimeCmp<int>::reverse); IntSet col2(reverse_order); fill(col2); PRINT_ELEMENTS(col2, "col2: "); col1= col2; col1.insert(3); PRINT_ELEMENTS(col1, "col1: "); if(col1.value_comp() == col2.value_comp()) { cout<<"col1 == col2"<<endl; } else { cout<<"col1 != col2"<<endl; } return 0; }
注:比较操作只能比较类型相同的容器。不同类型的容器比较会出错!!
#include <iostream> #include <set> #include <algorithm> #include <iterator> using namespace std; int main(int argc, char *argv[]) { typedef set<int, greater<int> > IntSet; IntSet col1; col1.insert(4); col1.insert(3); col1.insert(5); col1.insert(1); col1.insert(6); col1.insert(2); col1.insert(5); IntSet::iterator pos; for(pos = col1.begin(); pos != col1.end(); ++pos) { cout<< *pos << " "; } cout<< endl; pair<IntSet::iterator, bool> status = col1.insert(4); if(status.second) { cout<< "4 inset as a element " << distance(col1.begin(), status.first) + 1 << endl; } else cout<< "4 already exists "<<endl; set<int> col2(col1.begin(), col1.end()); copy(col2.begin(),col2.end(), ostream_iterator<int>(cout," ")); cout<<endl; col2.erase(col2.begin(), col2.find(3)); int num; num = col2.erase(5); cout<<num<<"element(s) removed"<<endl; copy(col2.begin(),col2.end(), ostream_iterator<int>(cout," ")); cout<<endl; return 0; }运行结果