【发布时间】:2017-08-11 22:31:14
【问题描述】:
我知道防止重复的一个好方法是使用unordered_set。但是,当我想要unordered_set<vector<string>> 时,这种方法似乎不起作用。我该怎么做呢?例如,我想防止<"a", "b", "c"> 在我的unordered_set<vector<string>> 中重复。
这个unordered_set<vector<string>> 也可以在定义的类之外使用吗?
代码:
unordered_set<vector<string>> abc({"apple", "ball", "carrot"});
abc.insert({"apple", "ball", "carrot"});
cout << abc.size() << endl; //abc.size() should be 1
【问题讨论】:
-
我想我必须自己定义一个哈希?虽然不知道怎么做
-
你能发布一个非常小的例子,它添加 {"a", "b", "c"} 两次,并检查集合的 size() 吗?
-
简单地使用 std::set 怎么样?
-
我收到了一个类似的错误,提示“C++ 标准没有为这种类型提供哈希值。”似乎很清楚。查找模板参数以了解如何添加哈希器。我只是用一个很少使用的字符作为连接符连接所有字符串。如果连接符是 '+',它不会知道 {"a+b+c"} 和 {"a", "b", "c"} 之间的区别,但也许你知道没有使用 '\0'在任何字符串中? (虽然 xyz 似乎很可能)
标签: c++ vector unordered-set