Given std::unordered_set<Key, Hash>
for arbitrary hashable Key
, you could define
template<class Key, class Hash = std::hash<Key>>
bool operator< (std::unordered_set<Key, Hash> const& L, std::unordered_set<Key, Hash> const& R)
{
return std::lexicographical_compare(
begin(L), end(L), begin(R), end(R),
[](Key const& kL, Key const& kR) {
return Hash()(kL) < Hash()(kR);
});
}
which will use the ordering on hash indices of Key
. You can then define an ordering on item
bool operator< (item const& L, item const& R)
{
return std::tie(L.sortI, L.sortUS) < std::tie(R.sortI, R.sortUS);
}
and std::tie
will make a std::tuple
out of references to the members of your item
so that you can use the operator<
from std::tuple
.
NOTE: you can easily prove that the above comparison is a StrictWeakOrder (a requirement for std::sort
) since both the std::tuple
comparison and the lexicographical_compare
have this property.
However, the ordering of unordered_set
is very unusual in other respects.
- the hashed key index doesn't correspond to the order in which you iterate over elements (there is some modulo operation that maps hashed keys to indices in the container)
- adding elements to an
unordered_set
can result in rehashing and invalidation of previous orderering