C++ examples for STL:set
Storing pointers in a set
#include <iostream> #include <set> #include <string> #include <functional> #include <cassert> using namespace std; // Class for comparing strings given two string pointers struct strPtrLess { bool operator()(const string* p1, const string* p2) { assert(p1 && p2);/*w w w .j a va 2 s . c o m*/ return(*p1 < *p2); } }; int main() { set<string*, strPtrLess> setStrPtr; // Give it my special less-than functor string s1 = "T"; string s2 = "D"; string s3 = "H"; setStrPtr.insert(&s1); setStrPtr.insert(&s2); setStrPtr.insert(&s3); for (set<string*, strPtrLess>::const_iterator p = setStrPtr.begin(); p != setStrPtr.end(); ++p) cout << **p << endl; // Dereference the iterator and what it points to }