如何确定两个向量的区别?
我有vector<int> v1
和vector<int> v2
;
我想要得到一个vector<int> vDifferences
,其中只包含v1
或v2
中独有的元素。
是否有标准的方法来实现这一点?
这里是完整且正确的答案。在使用set_symmetric_difference
算法之前,源范围必须被排序:
using namespace std; // For brevity, don't do this in your own code...
vector<int> v1;
vector<int> v2;
// ... Populate v1 and v2
// For the set_symmetric_difference algorithm to work,
// the source ranges must be ordered!
vector<int> sortedV1(v1);
vector<int> sortedV2(v2);
sort(sortedV1.begin(),sortedV1.end());
sort(sortedV2.begin(),sortedV2.end());
// Now that we have sorted ranges (i.e., containers), find the differences
vector<int> vDifferences;
set_symmetric_difference(
sortedV1.begin(),
sortedV1.end(),
sortedV2.begin(),
sortedV2.end(),
back_inserter(vDifferences));
// ... do something with the differences
C++11为我们提供了一些能力,通过标准化
- 将每个容器加载到哈希表中。
- 如果两个容器的大小不同,则较小的那个哈希表将用于步骤3中的遍历。否则,将使用两个哈希表中的第一个。
- 遍历第2步中选择的哈希表,检查每个项目是否存在于两个哈希表中。如果是,则从两个哈希表中删除它。之所以优先选择较小的哈希表进行遍历,是因为无论容器大小如何,哈希表查找的平均时间复杂度都是O(1)。因此,遍历的时间是n的线性函数(即O(n)),其中n是正在遍历的哈希表的大小。
- 对哈希表中剩余的项目进行并集,并将结果存储在差异容器中。
unordered_multiset
容器来实现此类解决方案。我还使用了auto
关键字的新用法,以使以下基于哈希表的解决方案更加简洁:using namespace std; // For brevity, don't do this in your own code...
// The remove_common_items function template removes some and / or all of the
// items that appear in both of the multisets that are passed to it. It uses the
// items in the first multiset as the criteria for the multi-presence test.
template <typename tVal>
void remove_common_items(unordered_multiset<tVal> &ms1,
unordered_multiset<tVal> &ms2)
{
// Go through the first hash table
for (auto cims1=ms1.cbegin();cims1!=ms1.cend();)
{
// Find the current item in the second hash table
auto cims2=ms2.find(*cims1);
// Is it present?
if (cims2!=ms2.end())
{
// If so, remove it from both hash tables
cims1=ms1.erase(cims1);
ms2.erase(cims2);
}
else // If not
++cims1; // Move on to the next item
}
}
int main()
{
vector<int> v1;
vector<int> v2;
// ... Populate v1 and v2
// Create two hash tables that contain the values
// from their respective initial containers
unordered_multiset<int> ms1(v1.begin(),v1.end());
unordered_multiset<int> ms2(v2.begin(),v2.end());
// Remove common items from both containers based on the smallest
if (v1.size()<=v2.size)
remove_common_items(ms1,ms2);
else
remove_common_items(ms2,ms1);
// Create a vector of the union of the remaining items
vector<int> vDifferences(ms1.begin(),ms1.end());
vDifferences.insert(vDifferences.end(),ms2.begin(),ms2.end());
// ... do something with the differences
}
为了保持原有的顺序,步骤4比之前的解决方案更加昂贵,特别是如果要移除的物品数量很大。这是因为:
- 将每个容器加载到哈希表中。
- 如果两个容器大小不同,则较小的哈希表将用于第3步的遍历。否则,将使用两者中的第一个。
- 遍历在第2步中选择的哈希表,检查每个项目是否存在于两个哈希表中。如果存在,则从两个哈希表中删除它。
- 为了形成差异容器,按顺序遍历原始容器(即先遍历第一个容器再遍历第二个容器)。在其各自的哈希表中查找每个容器中的项目。如果找到,则将该项添加到差异容器中并从其哈希表中删除。不在各自哈希表中的项目将被跳过。因此,只有出现在哈希表中的项目才会出现在差异容器中,并且它们的出现顺序与它们在原始容器中的顺序相同,因为这些容器规定了最终遍历的顺序。
您是否需要从v1
和v2
中获取唯一且不在另一个序列中的元素?这听起来像是std::set_symmetric_difference。
将不在范围[first2,last2)中的范围[first1,last1)的元素以及不在范围[first1,last1)中的范围[first2,last2)的元素复制到以result开头的范围中。所构建的范围内的元素已排序。
template <class InputIt1, class InputIt2, class OutputIt> OutputIt set_difference(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt d_first);
该函数计算两个已排序范围的元素差集,将结果存储在输出迭代器所指向的范围中。参数:first1
,last1
:表示第一个已排序范围的起始和结束位置。first2
,last2
:表示第二个已排序范围的起始和结束位置。d_first
:输出迭代器,指向目标范围的起始位置。v1
和v2
的类型是一种选择,并且您只关心每个元素是否存在,请考虑在第一次使用std::vector
之前使用std::unordered_set
或std::set
。 - aschepler