#include<iostream> #include<list> #include<set> #include<unordered_set> #include<map> #include<algorithm> #include<typeinfo> #include<cassert> using namespace std; template<typename T> void print(const T &container); template<typename T> void printmap(const T &map); template<typename T> void displaySamp(const T &conSamp); template<typename T> struct Comp; template<typename T> struct Samp; template<typename T> struct compStru; bool compFunc(const int& it1, const int& it2) { return abs(it1-5)<abs(it2-5); } bool sortbyAlpha(const Samp<int> &it1, const Samp<int> &it2); template<typename T=int> struct Samp { T ival; char ch; Samp(T i=0, char c='@'):ival(i),ch(c) { } // //used by list::remove(); bool operator==(const Samp &rhs) const { return (ival==rhs.ival); } // //used by list::sort(); //以下[重载 <]若变更为[重载 >],将不能起到效果 bool operator<(const Samp &rhs) const { return (ival>rhs.ival); } //()的重载, 调用见 for_each() void operator()(const Samp<T> &sa) const {cout<<'('<<sa.ival<<", "<<sa.ch<<')'<<endl;} template<typename U> friend void print(const Samp<U> &); }; bool sortbyAlpha(const Samp<int> &it1, const Samp<int> &it2){return (it1.ch<it2.ch);} template<typename T=int> struct compStru { bool operator()(const T& lhs, const T& rhs) { return abs(lhs-5)<abs(rhs-5); } }; template<typename T=Samp<int>> struct compStru2 { bool operator()(const T& lhs, const T& rhs) { return abs(lhs.ival-5)<abs(rhs.ival-5); } }; int main () { list<int> a={1,4,5,6,3,2,6,7,9}; print(a); // // list::sort();之第二种[二元谓词做参数]; //若调用 "a.sort(compStru);", 则 error. a.sort(compFunc); print(a); a.remove(4); print(a); cout<<"----------------------------------\n"; list<Samp<int>> expe= { Samp<int>{1, 'F'}, Samp<int>{4, 'D'}, Samp<int>{3, 'H'}, Samp<int>{4, 'B'} }; displaySamp(expe); expe.sort(); displaySamp(expe); expe.sort(sortbyAlpha); displaySamp(expe); expe.remove(4); //4 converts to Samp<int>{4, '@'} displaySamp(expe); cout<<"=========================================\n"; // //using default sort predicate: less<int> set<int> b1={1,4,5,6,3,2,6,7,9}; print(b1); // //using user-defined sort predicate. set<int,compStru<int>> b2={1,4,5,6,3,2,6,7,9}; print(b2); //here: set={compStru(x): x \in {1,4,5,6,3,2,6,7,9}}. cout<<"Size() of b2 is: "<<b2.size()<<endl; cout<<"----------------------------------\n"; /* unordered_multiset<Samp<int>> tem{1,'B'}; //error */ multiset<Samp<int>> expe2= { Samp<int>{4, 'D'}, Samp<int>{3, 'A'}, Samp<int>{6, 'B'} }; displaySamp(expe2); /* sort(expe2.cbegin(),expe2.cend(),sortbyAlpha); //error */ expe2.erase(4); // //等价(作用相当)于 displaySamp(expe2); for_each(expe2.cbegin(),expe2.cend(),Samp<int>()); cout<<"----------------------------------\n"; multiset<Samp<int>,compStru2<Samp<int>>> expe3={ Samp<int>{4, 'D'}, Samp<int>{3, 'A'}, Samp<int>{6, 'B'} }; displaySamp(expe3); expe3.erase(4); // displaySamp(expe3); cout<<"\n=========================================\n"; multimap<int,string,compStru<int>>c={ {2, "Tony"}, {1, "Jack"}, {3, "Lee"}, {100, "Doe"}, {7, "Lucy"}, {3, "Wane"} }; printmap(c); /* sort(c.begin(),c.end(),compFunc); //error printmap(c); */ return 0; } template<typename T> struct Comp { bool operator()(const T& it1, const T& it2)const { return diff6(it1)<diff6(it2); } }; template<typename T> void print(const T &container) { for(typename T::const_iterator cp=container.cbegin();cp!=container.cend();cp++) cout<<*cp<<" "; cout<<endl; //for(auto &v:v) } template<typename T> void printmap(const T &map) { for(auto &v:map) cout<<v.first<<"\t-->\t"<<v.second<<endl; } template<typename U> void print(const Samp<U> &sa) { cout<<'('<<sa.ival<<", "<<sa.ch<<')'<<endl; } template<typename T> void displaySamp(const T &conSamp) { for(auto &v:conSamp) print(v); cout<<endl; }
运行结果
1 4 5 6 3 2 6 7 9 5 4 6 6 3 7 2 1 9 5 6 6 3 7 2 1 9 ---------------------------------- (1, F) (4, D) (3, H) (4, B) (4, D) (4, B) (3, H) (1, F) (4, B) (4, D) (1, F) (3, H) (1, F) (3, H) ========================================= 1 2 3 4 5 6 7 9 5 4 3 2 1 Size() of b2 is: 5 ---------------------------------- (6, B) (4, D) (3, A) (6, B) (3, A) ---------------------------------- (4, D) (6, B) (3, A) (3, A) ========================================= 3 --> Lee 7 --> Lucy 3 --> Wane 2 --> Tony 1 --> Jack 100 --> Doe