STL set中有set_union(取两集合并集)、set_intersection(取两集合交集)、set_difference(取两集合差集)。
1、这几个函数的参数一样。
2、set_union(x1.begin(), x1.end(), x2.begin(), x2.end(), inserter(x, x.begin())),前两个参数是集合x1的头尾,再依次是集合x2的头尾,最后一个参数就是将集合x1和集合x2取合集后存入集合x中。
当使用set_intersection()规定两个区间必须是有序的。
具体介绍如下:
set_intersection
两个排序范围的交点
构造一个从result指向的位置开始的排序范围,以及两个排序范围的集合交集。 两组的交集仅由两组中存在的元素形成。函数复制的元素始终来自第一个范围,顺序相同。 使用第一个版本的元素和第二个版本的comp进行比较。两个元素,[first1,last1)
[first2,last2)
operator<
一个 和 b如果(!(a<b) && !(b<a))
或如果,则视为等效(!comp(a,b) && !comp(b,a))
。
范围内的元素必须按照相同的标准(operator<
或comp)进行排序。结果范围也根据此进行排序。(即两个集合的排列大小的顺序必须相同)
此函数模板的行为等效于:
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result)
{
while (first1!=last1 && first2!=last2)
{
if (*first1<*first2) ++first1;
else if (*first2<*first1) ++first2;
else {
*result = *first1;
++result; ++first1; ++first2;
}
}
return result;
}
参数
first1,last1
将迭代器输入到第一个排序序列的初始位置和最终位置。使用的范围是[first1,last1)
,包含first1和last1之间的所有元素,包括first1指向的元素,但不包括last1指向的元素。
first2,last2
将迭代器输入到第二个排序序列的初始位置和最终位置。使用的范围是[first2,last2)
。
结果
将迭代器输出到存储结果序列的范围的初始位置。
指向类型应支持从第一个范围分配元素的值。
参数
first1,last1
将迭代器输入到第一个排序序列的初始位置和最终位置。使用的范围是[first1,last1)
,包含first1和last1之间的所有元素,包括first1指向的元素,但不包括last1指向的元素。
first2,last2
将迭代器输入到第二个排序序列的初始位置和最终位置。使用的范围是[first2,last2)
。
结果
将迭代器输出到存储结果序列的范围的初始位置。
指向类型应支持从第一个范围分配元素的值。
返回值
构造范围末尾的迭代器。
// set_intersection example
#include <iostream> // std::cout
#include <algorithm> // std::set_intersection, std::sort
#include <vector> // std::vector
int main () {
int first[] = {5,10,15,20,25};
int second[] = {50,40,30,20,10};
std::vector<int> v(10); // 0 0 0 0 0 0 0 0 0 0
std::vector<int>::iterator it;
std::sort (first,first+5); // 5 10 15 20 25
std::sort (second,second+5); // 10 20 30 40 50
it=std::set_intersection (first, first+5, second, second+5, v.begin());
// 10 20 0 0 0 0 0 0 0 0
v.resize(it-v.begin()); // 10 20
std::cout << "The intersection has " << (v.size()) << " elements:\n";
for (it=v.begin(); it!=v.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
输出10 20
set_union
并集
联合两个排序范围
构造一个从result指向的位置开始的排序范围,以及两个排序范围的集合[first1,last1)
和[first2,last2)
。
两组的并集由存在于任一组中的元素形成,或者由两者组成。第二个范围中具有第一个范围中的等效元素的元素不会复制到结果范围。
使用operator<
第一个版本的元素和第二个版本的comp进行比较。两个元素,一个 和 b如果(!(a<b) && !(b<a))
或如果,则视为等效(!comp(a,b) && !comp(b,a))
。
范围内的元素必须按照相同的标准(operator<
或comp)进行排序。结果范围也根据此进行排序。
此函数模板的行为等效于:
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_union (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result)
{
while (true)
{
if (first1==last1) return std::copy(first2,last2,result);
if (first2==last2) return std::copy(first1,last1,result);
if (*first1<*first2) { *result = *first1; ++first1; }
else if (*first2<*first1) { *result = *first2; ++first2; }
else { *result = *first1; ++first1; ++first2; }
++result;
}
}
first1,last1
将迭代器输入到第一个排序序列的初始位置和最终位置。使用的范围是[first1,last1)
,包含first1和last1之间的所有元素,包括first1指向的元素,但不包括last1指向的元素。
first2,last2
将迭代器输入到第二个排序序列的初始位置和最终位置。使用的范围是[first2,last2)
。
结果
将迭代器输出到存储结果序列的范围的初始位置。
指向的类型应支持从其他范围分配元素的值。
范围不得重叠。
返回值
构造范围末尾的迭代器。
// set_union example
#include <iostream> // std::cout
#include <algorithm> // std::set_union, std::sort
#include <vector> // std::vector
int main () {
int first[] = {5,10,15,20,25};
int second[] = {50,40,30,20,10};
std::vector<int> v(10); // 0 0 0 0 0 0 0 0 0 0
std::vector<int>::iterator it;
std::sort (first,first+5); // 5 10 15 20 25
std::sort (second,second+5); // 10 20 30 40 50
it=std::set_union (first, first+5, second, second+5, v.begin());
// 5 10 15 20 25 30 40 50 0 0
v.resize(it-v.begin()); // 5 10 15 20 25 30 40 50
std::cout << "The union has " << (v.size()) << " elements:\n";
for (it=v.begin(); it!=v.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
输出5 10 15 20 25 30 40 50
set_difference
差集
两个排序范围的差异
构造从result指向的位置开始的排序范围,其中排序范围相对于排序范围的设置差异。 两组的差异由第一组中存在的元素形成,但不存在于第二组中。函数复制的元素始终来自第一个范围,顺序相同。 对于支持多次出现的值的容器,差异包括与第一个范围中一样多的给定值的出现,减去第二个保留顺序中的匹配元素的数量。 请注意,这是一个方向性操作 - 对于对称的等效操作,请参阅[first1,last1)
[first2,last2)
set_symmetric_difference。
使用operator<
第一个版本的元素和第二个版本的comp进行比较。两个元素,一个 和 b如果(!(a<b) && !(b<a))
或如果,则视为等效(!comp(a,b) && !comp(b,a))
。
范围内的元素必须按照相同的标准(operator<
或comp)进行排序。结果范围也根据此进行排序。
此函数模板的行为等效于
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result)
{
while (first1!=last1 && first2!=last2)
{
if (*first1<*first2) { *result = *first1; ++result; ++first1; }
else if (*first2<*first1) ++first2;
else { ++first1; ++first2; }
}
return std::copy(first1,last1,result);
}
first1,last1
将迭代器输入到第一个排序序列的初始位置和最终位置。使用的范围是[first1,last1)
,包含first1和last1之间的所有元素,包括first1指向的元素,但不包括last1指向的元素。
first2,last2
将迭代器输入到第二个排序序列的初始位置和最终位置。使用的范围是[first2,last2)
。
结果
将迭代器输出到存储结果序列的范围的初始位置。
指向类型应支持从第一个范围分配元素的值。
范围不得重叠。
返回值
构造范围末尾的迭代器。
// set_difference example
#include <iostream> // std::cout
#include <algorithm> // std::set_difference, std::sort
#include <vector> // std::vector
int main () {
int first[] = {5,10,15,20,25};
int second[] = {50,40,30,20,10};
std::vector<int> v(10); // 0 0 0 0 0 0 0 0 0 0
std::vector<int>::iterator it;
std::sort (first,first+5); // 5 10 15 20 25
std::sort (second,second+5); // 10 20 30 40 50
it=std::set_difference (first, first+5, second, second+5, v.begin());
// 5 15 25 0 0 0 0 0 0 0
v.resize(it-v.begin()); // 5 15 25
std::cout << "The difference has " << (v.size()) << " elements:\n";
for (it=v.begin(); it!=v.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
输出:
差异有3个要素:
5 15 25 |
set_symmetric_difference
对称差集
两个排序范围的对称差异
构造一个从result指向的位置开始的排序范围,以及两个排序范围的设置对称差异。 两组的对称差异由一组中存在的元素形成,而另一组中不存在。在每个范围中的等效元素中,丢弃的那些是在调用之前以现有顺序出现的那些元素。还会为复制的元素保留现有顺序。 使用第一个版本的元素和第二个版本的comp进行比较。两个元素,[first1,last1)
[first2,last2)
operator<
一个 和 b如果(!(a<b) && !(b<a))
或如果,则视为等效(!comp(a,b) && !comp(b,a))
。
范围内的元素必须按照相同的标准(operator<
或comp)进行排序。结果范围也根据此进行排序。
此函数模板的行为等效于:
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_symmetric_difference (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result)
{
while (true)
{
if (first1==last1) return std::copy(first2,last2,result);
if (first2==last2) return std::copy(first1,last1,result);
if (*first1<*first2) { *result=*first1; ++result; ++first1; }
else if (*first2<*first1) { *result = *first2; ++result; ++first2; }
else { ++first1; ++first2; }
}
}
参数
first1,last1
将迭代器输入到第一个排序序列的初始位置和最终位置。使用的范围是[first1,last1)
,包含first1和last1之间的所有元素,包括first1指向的元素,但不包括last1指向的元素。
first2,last2
将迭代器输入到第二个排序序列的初始位置和最终位置。使用的范围是[first2,last2)
。
结果
将迭代器输出到存储结果序列的范围的初始位置。
指向的类型应支持从其他范围分配元素的值
范围不得重叠。
返回值
构造范围末尾的迭代器。
// set_symmetric_difference example
#include <iostream> // std::cout
#include <algorithm> // std::set_symmetric_difference, std::sort
#include <vector> // std::vector
int main () {
int first[] = {5,10,15,20,25};
int second[] = {50,40,30,20,10};
std::vector<int> v(10); // 0 0 0 0 0 0 0 0 0 0
std::vector<int>::iterator it;
std::sort (first,first+5); // 5 10 15 20 25
std::sort (second,second+5); // 10 20 30 40 50
it=std::set_symmetric_difference (first, first+5, second, second+5, v.begin());
// 5 15 25 30 40 50 0 0 0 0
v.resize(it-v.begin()); // 5 15 25 30 40 50
std::cout << "The symmetric difference has " << (v.size()) << " elements:\n";
for (it=v.begin(); it!=v.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
编辑并运行
输出:
对称差异有6个要素: 5 15 25 30 40 50 |
merge
合并已排序的范围
结合的元素在排序的区间[first1,last1)
和[first2,last2)
,进入一个新的开始范围在结果与它的所有元素进行排序。
使用operator<
第一个版本的元素和第二个版本的comp进行比较。两个范围内的元素必须按照相同的标准(operator<
或comp)进行排序。结果范围也根据此进行排序。
此函数模板的行为等效于:
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator merge (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result)
{
while (true) {
if (first1==last1) return std::copy(first2,last2,result);
if (first2==last2) return std::copy(first1,last1,result);
*result++ = (*first2<*first1)? *first2++ : *first1++;
}
}
参数
first1,last1
将迭代器输入到第一个排序序列的初始位置和最终位置。使用的范围是[first1,last1)
,包含first1和last1之间的所有元素,包括first1指向的元素,但不包括last1指向的元素。
first2,last2
将迭代器输入到第二个排序序列的初始位置和最终位置。使用的范围是[first2,last2)
。
结果
将迭代器输出到存储结果组合范围的范围的初始位置。它的大小等于上述两个范围的总和。
范围不得重叠。
两个输入范围中的元素应该可以分配给result指向的范围内的元素。它们也应该具有可比性(operator<
对于(1),对于(2)的comp)。
返回值
一个迭代器,指向结果序列中的past-the-end元素。
// merge algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::merge, std::sort
#include <vector> // std::vector
int main () {
int first[] = {5,10,15,20,25};
int second[] = {50,40,30,20,10};
std::vector<int> v(10);
std::sort (first,first+5);
std::sort (second,second+5);
std::merge (first,first+5,second,second+5,v.begin());
std::cout << "The resulting vector contains:";
for (std::vector<int>::iterator it=v.begin(); it!=v.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
编辑并运行
输出:
得到的载体含有:5 10 10 15 20 20 25 30 40 50 |