Ordered lists have the same member functions as unordered lists, but
elements are inserted in some (user-defined) order. Because some ordering is
required, you have to provide member functions to enforce an ordering
if you want to use your own elements in an ordered list. These functions are
operator<
and operator==
. Some other `external' functions has
to be provided also to print an element to a stream, see the
operator<<
in the example.
The example:
// olist.cc #include <hush/list.h> #include <iostream.h> // define our own class which can be used in the ordered list class myclass { public: myclass(int i) { i_ = i; } int value() const { return i_; } int operator<(const myclass& m) const { return (i_ < m.value()); } int operator==(const myclass& m) const { return (i_ == m.value()); } private: int i_; }; // you MUST define a stream-operator for your own class (but it's only used // when you evoke 'print' on an ordered list): ostream& operator<<(ostream& os, const myclass& mc) { os << mc.value(); return os; } int main() { myclass a(27), b(3), c(72); olist<myclass> l1; // create an ordered list with // elements of our own class l1.insert(&a); // add elements in unsorted order l1.insert(&b); l1.insert(&c); iter<myclass> mciter = l1; myclass* mcptr = 0; while (mcptr = mciter()) { cout << "item = " << mcptr -> value() << endl; } l1.print(cout); // print the entire list on cout, // great for debugging return 0; }