未排序向量上的STL set_union和set_interction

STL set_union and set_intersection on unsorted vectors

本文关键字:set union interction STL 排序 向量      更新时间:2023-10-16

下面是我所做的关于集合交集和并集测试的代码。我不明白为什么我注释掉排序函数时输出不正确。为什么这种类型是必要的?还是我这里遗漏了什么?让程序员负责首先对输入进行排序的技术原因是什么?这是为了保证排序不会进行两次吗(如果向量已经排序,算法会再次排序…)?

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void test_vector_union();
void test_vector_intesection();
int main(int i, char * args [])
{
  cout <<endl<< "test union of unsorted vectors {1,4,3,2,0} and {6,10,2,1,4}" << endl;
  test_vector_union();
  cout <<endl<< "test intersection of unsorted vectors {1,4,3,2,0} and {6,10,2,1,4}" <<     endl;
  test_vector_intesection(); 
  return 0;
 }
void test_vector_union(){
 vector<int> x = {1,4,3,2,0};
 vector<int> y= {6,10,2,1,4};
//sort(x.begin(),x.end()); sort(y.begin(),y.end());
vector<int> z ;
set_union(x.begin(),x.end(),y.begin(),y.end(),back_inserter(z));
for (int i:z)
     cout << i <<",";
}
void test_vector_intesection(){
 vector<int> x = {1,4,3,2,0};
 vector<int> y= {6,10,2,1,4};
 //sort(x.begin(),x.end()); sort(y.begin(),y.end());
 vector<int> z ;
 set_intersection(x.begin(),x.end(),y.begin(),y.end(),back_inserter(z));
 for (int i:z)
     cout << i <<",";

}

因为这是std::set_union:的要求

构造从d_first开始的排序范围,该排序范围由一个或两个排序范围[first1,last1)和[first2,last2)中存在的所有元素组成。

1) 期望两个输入范围都用运算符<

2) 期望它们使用给定的比较函数comp 进行排序

(重点矿井)

您不应该在未排序的范围上调用这些算法。

如果您关心标准中的官方措辞(§25.4.5/1):

本节定义了排序结构上的所有基本集合操作。它们还处理包含等效元素的多个副本的多集(23.4.7)。集合操作的语义以标准的方式被推广到多集合,定义set_union()包含每个元素的最大出现次数,set_interaction()包含最小出现次数,等等

该部分包括std::includesstd::set_unionstd::set_intersectionstd::set_differencestd::set_symmetric_difference的规范,因此对排序输入的要求适用于所有这些算法。

这是必需的。这是std::union的文档摘录。:-

Constructs a sorted range beginning at d_first consisting of all elements present in one or both sorted ranges [first1, last1) and [first2, last2).
1) Expects both input ranges to be sorted with operator<
2) Expects them to be sorted with the given comparison function comp
If some element is found m times in [first1, last1) and n times in [first2, last2), then all m elements will be copied from [first1, last1) to d_first, preserving order, and then exactly std::max(n-m, 0) elements will be copied from [first2, last2) to d_first, also preserving order.