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