STL容器的并集(set_union)、交集(set_intersection)和差集(set_difference)函数的使用

13 篇文章 1 订阅

集合的交集并集和差集并不是只能用于set对象,STL提供了支持这些操作的算法,它们是通用函数。然而所有set对象都自动满足使用这些算法的先决条件—容器必须先经过排序
再三注意,使用这三个函数之前,容器必须先进行排序

  • set_union()
  • set_intersection()
  • set_difference()
    这三个函数的接口都相同,接受五个迭代器参数。前两个迭代器定义了第一个容器的区间,接下来两个迭代器定义了第二个容器的区间,最后一个迭代器是输出迭代器,指出将结果集合复制到什么位置。这几个函数的前四个参数一样,只有第五个参数有多重版本。

EX1:set_union(A.begin(),A.end(),B.begin(),B.end(),inserter( C1 , C1.begin() ) );
前四个参数依次是第一的集合的头尾,第二个集合的头尾。第五个参数的意思是将集合A、B取合集后的结果存入集合C中。

EX2:set_union(A.begin(),A.end(),B.begin(),B.end(),ostream_iterator(cout," “));这里的第五个参数的意思是将A、B取合集后的结果直接输出,(cout," ")双引号里面是输出你想用来间隔集合元素的符号或是空格。

下面是set_union的原型:
template<class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_union(
InputIterator1_First1 ,
InputIterator1_Last1 ,
InputIterator2_First2 ,
InputIterator2_Last2 ,
OutputIterator_Result
);
链接: set_union.

// 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;
}

链接:set_intersection.

// 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;
}

链接:set_difference.

// 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;
}

关于第五个迭代器参数

在上面的案例中,第五的参数为v.begin(),这要求容器v必须先被分配好了足够的空间。如果v为空则不行。于是可以使用模板迭代器ostream_iteratorinsert_iterator来解决问题.

使用ostream_iterator直接输出

// set_union example
#include <iostream>     // std::cout
#include <algorithm>    // std::set_union, std::sort
#include <vector>       // std::vector
#include<string>
#include<iterator>
#include<algorithm>
using namespace std;
int main() {
		vector<string> set1{ "buffoon","thinkers","for","heavy","can","for" };
		vector<string> set2{ "metal","any","food","elegant","deliver","for" };
		vector<string> result;
		vector<string>::iterator it;
		ostream_iterator<string, char> out(cout, " ");
	//使用前必须先排序
	sort(set1.begin(), set1.end());     
	sort(set2.begin(), set2.end());   

	copy(set1.begin(), set1.end(),out);//buffoon can for for heavy thinkers
	cout << "" << endl;
	copy(set2.begin(), set2.end(),out);//any deliver elegant food for metal
	cout << "" << endl;

	set_union(set1.begin(), set1.end(), set2.begin(), set2.end(), out);
	//并集结果:any buffoon can deliver elegant food for for heavy metal thinkers
	return 0;
}

使用insert_iterator插入到新的容器中

// set_union example
#include <iostream>     // std::cout
#include <algorithm>    // std::set_union, std::sort
#include <vector>       // std::vector
#include<string>
#include<iterator>
#include<algorithm>
using namespace std;
int main() {
		vector<string> set1{ "buffoon","thinkers","for","heavy","can","for" };
		vector<string> set2{ "metal","any","food","elegant","deliver","for" };
		vector<string> result;
		vector<string>::iterator it;
		ostream_iterator<string, char> out(cout, " ");

		insert_iterator<vector<string> > inserter(result,result.begin());

	//使用前必须先排序
	sort(set1.begin(), set1.end());     
	sort(set2.begin(), set2.end());   

	copy(set1.begin(), set1.end(),out);//buffoon can for for heavy thinkers
	cout << "" << endl;
	copy(set2.begin(), set2.end(),out);//any deliver elegant food for metal
	cout << "" << endl;

	//将求并集的结果放入result
	set_union(set1.begin(), set1.end(), set2.begin(), set2.end(), inserter);

	copy(result.begin(), result.end(), out);
	//并集结果:any buffoon can deliver elegant food for for heavy metal thinkers
	return 0;
}
  • 4
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SOC罗三炮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值