//: C04:MultiSet1.cpp // From Thinking in C++, 2nd Edition // Available at http://www.BruceEckel.com // (c) Bruce Eckel 2000 // Copyright notice in Copyright.txt // Demonstration of multiset behavior #include #include #include #include using namespace std; class X { char c; // Used in comparison int i; // Not used in comparison // Don't need default constructor and operator= X(); X& operator=(const X&); // Usually need a copy-constructor (but the // synthesized version works here) public: X(char cc, int ii) : c(cc), i(ii) {} // Notice no operator== is required friend bool operator<(const X& x, const X& y) { return x.c < y.c; } friend ostream& operator<<(ostream& os, X x) { return os << x.c << ":" << x.i; } }; class Xgen { static int i; // Number of characters to select from: static const int span = 6; public: Xgen() { srand(time(0)); } X operator()() { char c = 'A' + rand() % span; return X(c, i++); } }; int Xgen::i = 0; typedef multiset Xmset; typedef Xmset::const_iterator Xmit; int main() { Xmset mset; // Fill it with X's: generate_n(inserter(mset, mset.begin()), 25, Xgen()); // Initialize a regular set from mset: set unique(mset.begin(), mset.end()); copy(unique.begin(), unique.end(), ostream_iterator(cout, " ")); cout << "\n----\n"; // Iterate over the unique values: for(set::iterator i = unique.begin(); i != unique.end(); i++) { pair p = mset.equal_range(*i); copy(p.first, p.second, ostream_iterator(cout, " ")); cout << endl; } } ///:~