c++ - 如何在 C++ 中只访问 unordered_set 的元素?

标签 c++ c++11 unordered-set

例如,

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/

相关文章:

c++ - C++ 编译器(或链接器?)如何知道如何处理 cpp 和头文件?

C++:通过引用传递或使用私有(private)实例变量

c++ - 将 boost::unordered_set 作为结果映射传递给 boost::split

c++ unordered_set .find 方法无法编译,给出 "expression must have class type"错误

c++ - 在 VCG 中细化网格

c++ - 谁可以告诉我一些有关 IPropertyBag::Read 的信息?

c++ - 使用 decltype 转换一个值,这可能吗?

c++ - 序列容器-为什么只有几个序列容器才支持一些表达式

c++ - 为几个可能的参数(类型名)专门化类

c++ - 通过哈希值和谓词搜索 std::unordered_set