getting "index" of set element via iterator

Use STL distance, namely std::distance(set.begin(), mySetIterator)

Please note that:

Returns the number of elements between first and last. The behavior is undefined if last is not reachable from first by (possibly repeatedly) incrementing first.

Remark : Complexity is linear;

However, if InputIt additionally meets the requirements of LegacyRandomAccessIterator, complexity is constant.


std::set and set::unordered_set are associative containers, not sequence containers, hence the concept itself of index doesn't make much sense.

If you need to retrieve an index for an associative container then design should be changed (even because without a concept of least or most recent inserted element the indices in such containers are subject to change).

Tags:

C++

Stl

Iterator