I have an unordered_set of a class ActiveStatusEffect
The set is declared as follows:
boost::unordered_set
unordered_set
doesn't have non-const iterators, because if you could mutate the item pointed to by an iterator you could violate the invariants of the set (uniqueness among others). Additionally if you sorted an unordered_set you would no longer be able to look up the item in the container anymore (assuming it works by hash).
If you really want to sort the set of items, you'll need to copy it into a vector
first and then sort that. But in that case have you considered if unordered_set
is the right container for you in the first place? What about using a normal set
which is ordered at the expense of lookups being slower.
boost::unordered_set<Foo> a;
a.insert(...);
...
std::set<Foo> b(a.begin(), a.end());
std::set<Foo> c;
std::copy(a.begin(), a.end(), std::inserter(c, c.end());
Voilà, a sorted set.