Search code examples
c++c++11unordered-set

how to access only element of unordered_set in c++?


For ex,

unordered_set<int> s ;
s.insert(100);

How do I get value 100 from s?

From http://www.cplusplus.com/reference/unordered_set/unordered_set/begin/,

Notice that an unordered_set object makes no guarantees on which specific element is considered its first element. But, in any case, the range that goes from its begin to its end covers all the elements in the container (or the bucket), until invalidated.

So s.begin() won't always give me value 100?

Please clarify.


Solution

  • When you have only one element (100), you don't have order issue, so *begin(s) is 100.

    But, as soon as you have 2 or more elements in unordered_set, you don't know which value would be the first one (*begin(s)).