如何保持一组没有重复的向量

How to maintain a set of vectors with no repetitions

本文关键字:向量 一组 何保持      更新时间:2023-10-16

我有一个c++程序,它产生了很多整数向量(每个向量都是排序的),它们的产生速度非常快,而且数量很多,我需要跟踪它们并删除重复的部分,最后把它们全部打印出来。我应该使用什么数据结构?

我尝试使用hash_mapunordered_map,但我得到了很多的错误,似乎他们不支持向量的哈希映射(所以他们如何支持字符串虽然?):

(.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提供一个散列函数来使用unordered_map。默认情况下提供string的哈希函数。或者,您可以使用std::set。std::set的元素保证没有重复。