Can't sort unordered_set

后端 未结 2 634
忘掉有多难
忘掉有多难 2021-01-27 03:59

I have an unordered_set of a class ActiveStatusEffect The set is declared as follows:

boost::unordered_set          


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-27 04:23

    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.

提交回复
热议问题