例如,
unordered_set<int> s ;
s.insert(100);
如何从 s 中获取值 100?
来自 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.
所以 s.begin()
不会总是给我值 100?
请澄清。
最佳答案
当你只有一个元素(100)时,你没有顺序问题,所以 *begin(s)
是 100
。
但是,一旦 unordered_set
中有 2 个或更多元素,您就不知道第一个值是哪个 (*begin(s)
) .
关于c++ - 如何在 C++ 中只访问 unordered_set 的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51187630/