// This file is one of a series that demonstrates how to write & use // a comparison functor for an STL container. // // This version is must like sort4.cc, but sorts by different criteria. #include #include #include #include using namespace std; struct compare { // Primary sort: by last digit // Secondary sort: value of number bool operator()(int a, int b) const { int last_a = a%10, last_b = b%10; if (last_a != last_b) return last_a < last_b; return a < b; } }; int main() { int values[] = {31, 41, 59, 26, 53, 58, 97, 93, 23, 84}; set s(values, values+10); copy(s.begin(), s.end(), ostream_iterator(cout, " ")); cout << '\n'; }