If the list is sorted, use its unique method.
If the list is not sorted (and you do not want to sort it):
set<string> found; for (list<string>::iterator x = the_list.begin(); x != the_list.end();) { if (!found.insert(*x).second) { x = the_list.erase(x); } else { ++x; } }
To avoid copying rows into a set:
struct less { template<class T> bool operator()(T &a, T &b) { return std::less<T>()(a, b); } }; struct deref_less { template<class T> bool operator()(T a, T b) { return less()(*a, *b); } }; void remove_unsorted_dupes(list<string> &the_list) { set<list<string>::iterator, deref_less> found; for (list<string>::iterator x = the_list.begin(); x != the_list.end();) { if (!found.insert(x).second) { x = the_list.erase(x); } else { ++x; } } }
Fred nurk
source share