#include #include #include #include #include using namespace std; template class Element { public: Element(const X& x_arg, const Y& y_arg, const Z& z_arg): x(x_arg), y(y_arg), z(z_arg) {} // public data members OK here since Elements will only be accessed by << and StrictWeakOrdering X x; Y y; Z z; }; template ostream& operator<< (ostream& os, const Element& e) { os << e.x << "\t" << e.y << "\t" << e.z << endl; return os; } class StrictWeakOrdering { public: template bool operator() (const Element& e1, const Element& e2) { return (e1.x < e2.x) || (e1.x == e2.x && e1.y < e2.y) || (e1.x == e2.x && e1.y == e2.y && e1.z < e2.z); } }; void main() { set, StrictWeakOrdering> s; int integers[] = {23,23,23,12,5,6,78,96,76,43}; float floats[] = {2.3,2.3,3.2,1.2,5.0,6.0,7.8,9.6,7.6,4.3}; string strings[] = {"one","two","three","four","five","six","seven","eight","nine","ten"}; for (int i = 0; i < sizeof(integers)/sizeof(int); i++) { s.insert(Element(integers[i], floats[i], strings[i])); } // print sorted sequence of elements copy(s.begin(), s.end(), ostream_iterator >(cout)); }