【发布时间】:2016-12-26 18:26:35
【问题描述】:
我知道人们在不关心集合中元素的顺序时会使用unordered_set。但是,当我在C++ Shell上运行示例程序时
#include <iostream>
#include <unordered_set>
#include <string>
int main()
{
std::unordered_set<std::string> inputSet;
inputSet.insert("Hello world");
inputSet.insert("Abcdef");
inputSet.insert("This is the test string...");
for(const auto &val : inputSet)
std::cout << val.c_str() << std::endl;
return 0;}
它给了我
This is the test string...
Abcdef
Hello world
我尝试运行它 3 或 4 次,它仍然给我相同的输出,这意味着 unordered_set 可以确定插入顺序。
谁能解释unordered_set如何确定插入顺序?
对不起,如果之前有人问过,我已经在网上搜索了一段时间,但我找不到这个问题的具体答案。提前致谢。
【问题讨论】:
-
为什么你还需要知道?在任何情况下,您都不应试图依赖订单。
-
@mclaassen 有时候我们应该有好奇心。
标签: c++ unordered-set