// (C) Porkolab 2003 // // A.6.42. // Specifying sorting criterion at runtime #include <iostream> #include <set> using namespace std; template <typename T> class RuntimeCmp { public: enum cmp_mode { normal, reverse }; 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& rhs) { return mode == rhs.mode; } private: cmp_mode mode; }; int main() { set<int, RuntimeCmp<int> > s1; // set with normal sorting RuntimeCmp<int> reverse(RuntimeCmp<int>::reverse); set<int, RuntimeCmp<int> > s2(reverse); // set with reverse sorting cout << "Same sorting: " << (s1.value_comp() == s2.value_comp()) << endl; s2 = s1; // copy with the sorting criteria cout << "Same sorting: " << (s1.value_comp() == s2.value_comp()) << endl; return 0; }