未排序向量上的STL set_union和set_interction
STL set_union and set_intersection on unsorted vectors
下面是我所做的关于集合交集和并集测试的代码。我不明白为什么我注释掉排序函数时输出不正确。为什么这种类型是必要的?还是我这里遗漏了什么?让程序员负责首先对输入进行排序的技术原因是什么?这是为了保证排序不会进行两次吗(如果向量已经排序,算法会再次排序…)?
#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::includes
、std::set_union
、std::set_intersection
、std::set_difference
和std::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.
相关文章:
- 为什么我无法更改"set<set>"循环中的值<int>
- 对于set上的循环-获取next元素迭代器
- C++Union/Struct位域的实现和可移植性
- 在声明中合并两个常量"std::set"(不是在运行时)
- 有没有办法对std::unordered_set、std::unrdered_map、std::set、std::map
- 将 std::set 与基于键的比较器一起使用
- 如何使用set实现无序数据结构?
- 使用运算符调用 void 函数时出错<set>
- 修改"std::set"中用户定义类型的值
- 如何通过多类"Union variable" (sfml) 使用轮询事件
- 错误:"Left of getValue must have class/struct/union"
- 生成提升::hana::set 的常量表达式问题
- 如何在构造函数参数中初始化"std::set"?
- 如何使用 lower_bound/upper_bound 从 std::set 获取索引号?
- 如何在 C++ 中转发声明 std::set?
- C++Union,两个活跃成员,仅简历资格不同
- 重构使用动态强制转换的 std::set 的比较运算符
- 如何以"union-like"方式更改C++向量的数据类型
- set::find 查找不存在的元素
- 我对set.union(C++)有问题