C++ Abstract Datatypes and STL

vector<>

 * The last element in the vector is  
 * Iterating beyond  yields arbitrary results
 * Check for membership of n in v:

List Iteration
using namespace std; class Thing { public: Thing(string text): text(text) {} protected: string text; // is declared as friend because it accesses 'text' friend ostream& operator <<(ostream& os, const Thing* t); }; ostream& operator <<(ostream& os, const Thing* t) { return os << t->text << endl; } int main(void) { list l;    for(int n = 0; n < 3; n++) l.push_back(new Thing("Thingie")); for(list::iterator i = l.begin; i != l.end; ++i) cout << *i; return 0; }
 * 1) include
 * 2) include
 * 3) include

Sorting
bool ordering_relation(Thing *a, Thing *b) { return a->value < b->value; }

vector things = new vector;

// sort does not maintain order of elements that are equal sort(things.begin, things.end, ordering_relation);

// stable_sort maintains order of equal elements stable_sort(things.begin, things.end, ordering_relation);

Complex Number
using namespace std; int main(void) { complex x(1,2); complex y = x / 2.f;    //y = x / 2.0; error: division with double //y = x / 2;  error: division with int cout << sin(y) << endl; cout << norm(y) << endl; return 0; }
 * 1) include
 * 2) include

Don't Inherit from STL Classes

 * It is a common question, "Why shouldn't we inherit a class from STL classes?" The answer to this question is simple—the Big Three rule [CLI95]. In other words, you should have a virtual destructor in a class if you want to make it a base class. However, there isn't any class in STL that defines a virtual destructor. Even if you try to inherit a class from STL class, the compiler doesn't complain about it, although there is a high chance of a resource leak in the program.

--quote from codeguru

Compose instead, e.g. include a list instance in your PrintableList template class:

using namespace std; template class PrintableList { public: operator string; void operator <<(T stuff) { l.push_back(stuff); } protected: vector l; }; template PrintableList::operator string { // vector::iterator i;      // this does not parse, even though it                                     // makes sense typename vector::iterator i; // 'typename' kicks the ass of the parser // to make it understand that it is a                                    // type name and not some other arbitrary // stuff. C++ is kinda stupid that way. string s("["); for(i = l.begin; i != l.end-1; ++i) // here it would make sense to        s += string(*i) + ", ";             // use the STL algorithm return s + string(*i++) + "]";         // 'accumulate'. Unfortunately, }                                          // it didn't exist in my GNU C++ // library at the time of writing. int main(void) { PrintableList l;    l << "hewey"; l << "dewey"; l << "luie"; cout << string(l); return 0; }
 * 1) include
 * 2) include
 * 3) include