//: C07:ListVsSet.cpp // From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. // (c) 1995-2004 MindView, Inc. All Rights Reserved. // See source code use permissions stated in the file 'License.txt', // distributed with the code package available at www.MindView.net. // Comparing list and set performance. #include #include #include #include #include #include #include #include "PrintContainer.h" using namespace std; class Obj { int a[20]; // To take up extra space int val; public: Obj() : val(rand() % 500) {} friend bool operator<(const Obj& a, const Obj& b) { return a.val < b.val; } friend bool operator==(const Obj& a, const Obj& b) { return a.val == b.val; } friend ostream& operator<<(ostream& os, const Obj& a) { return os << a.val; } }; struct ObjGen { Obj operator()() { return Obj(); } }; int main() { const int SZ = 5000; srand(time(0)); list lo; clock_t ticks = clock(); generate_n(back_inserter(lo), SZ, ObjGen()); lo.sort(); lo.unique(); cout << "list:" << clock() - ticks << endl; set so; ticks = clock(); generate_n(inserter(so, so.begin()), SZ, ObjGen()); cout << "set:" << clock() - ticks << endl; print(lo); print(so); } ///:~