c++ - 如何维护一组没有重复的 vector

标签 c++ data-structures vector hashmap

我有一个用 C++ 编写的程序,它会生成大量整数 vector (每个 vector 都已排序),并且它们的生成速度非常快且数量很大,我需要跟踪它们并删除重复项,最后全部打印出来。我应该使用什么数据结构?

我尝试使用 hash_mapunordered_map 但我遇到了很多错误,而且它们似乎不支持 vector 的 HashMap (那么它们如何支持字符串呢?):

(.text+0xdc56): undefined reference to `std::tr1::hash<std::vector<int, std::allocator<int> > >::operator()(std::vector<int, std::allocator<int> >) const'
objects/Prog.o: In function `std::tr1::_Hashtable<std::vector<int, std::allocator<int> >, std::pair<std::vector<int, std::allocator<int> > const, int>, std::allocator<std::pair<std::vector<int, std::allocator<int> > const, int> >, std::_Select1st<std::pair<std::vector<int, std::allocator<int> > const, int> >, std::equal_to<std::vector<int, std::allocator<int> > >, std::tr1::hash<std::vector<int, std::allocator<int> > >, std::tr1::__detail::_Mod_range_hashing, std::tr1::__detail::_Default_ranged_hash, std::tr1::__detail::_Prime_rehash_policy, false, false, true>::_M_rehash(unsigned long)':
Prog.cpp:(.text._ZNSt3tr110_HashtableISt6vectorIiSaIiEESt4pairIKS3_iESaIS6_ESt10_Select1stIS6_ESt8equal_toIS3_ENS_4hashIS3_EENS_8__detail18_Mod_range_hashingENSE_20_Default_ranged_hashENSE_20_Prime_rehash_policyELb0ELb0ELb1EE9_M_rehashEm[std::tr1::_Hashtable<std::vector<int, std::allocator<int> >, std::pair<std::vector<int, std::allocator<int> > const, int>, std::allocator<std::pair<std::vector<int, std::allocator<int> > const, int> >, std::_Select1st<std::pair<std::vector<int, std::allocator<int> > const, int> >, std::equal_to<std::vector<int, std::allocator<int> > >, std::tr1::hash<std::vector<int, std::allocator<int> > >, std::tr1::__detail::_Mod_range_hashing, std::tr1::__detail::_Default_ranged_hash, std::tr1::__detail::_Prime_rehash_policy, false, false, true>::_M_rehash(unsigned long)]+0x126): undefined reference to `std::tr1::hash<std::vector<int, std::allocator<int> > >::operator()(std::vector<int, std::allocator<int> >) const'
collect2: ld returned 1 exit status
make: *** [run] Error 1

还有其他方法可以解决这个问题吗?还有其他效率更高的数据结构吗?

最佳答案

需要为vector提供hash函数才能使用unordered_map。字符串的哈希函数是默认提供的。 或者,您可以使用 std::set。 std::set 的元素保证没有重复。

关于c++ - 如何维护一组没有重复的 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22623952/

相关文章:

c++ - CLI/C# 类中属性的特殊访问控制

c++ - 如何使用代码块 Ubuntu 编译 64 位 C++ 程序

c++ - 将从一个函数返回的 vector 传递给另一个函数

c++ - 使用 ifstream 时,Visual Studio 2017 中 Release 和 Debug 的输出不同?

r - 当无法确定 wilcox.test p 值时如何返回 "NA"(R)

regex - 将缩写与字典匹配的最快方法

java - O(1) 的映射同时搜索键和值

java - 寻找保持大小的数据结构并清除过程中的旧元素

c++ - std::vector 是否更改其地址?如何避免

c++ - 如何在 vector 中搜索结构项?