Understanding of the function of the intersection, union and difference of sets

Understanding of the function of the intersection, union and difference of sets

Introduction to the algorithm:

  • set_intersection // Find the intersection of two containers (intersection==>intersection, intersection, intersection, intersection, intersection)
  • set_union // Find the union of two containers (union==>union, union, union, union, union)
  • set_difference // Find the difference of two containers (difference==> difference, difference, difference, difference
Intersection

Features:

  • Find the intersection of two containers

Function prototype:

  • set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
  • // beg1 container 1 start iterator
    // end1 container 1 end iterator
    // beg2 container 2 start iterator
    // end2 container 2 end iterator
    // dest target container start iterator

It should be noted that these two sets must be ordered sequences, that is, they are all sorted

#include"head.h"


int main()
{
	vector<int>a = { 1,3,5,6,4 ,7};
	sort(a.begin(), a.end(), [](int a, int b)->bool {return a < b; });//此处只相当于复习了一下lambda表达式,因为之前只学过,没用过,在这里先试试,其实就相当于默认从小到大的排序;
		vector<int>b = { 2,3,6,2,7,9 };
		sort(b.begin(), b.end(), [](int a, int b) {return a < b; });
		vector<int>c(100);
		//cout << c.size();
	auto it=	set_intersection(a.begin(), a.end(), b.begin(), b.end(), c.begin());
	for_each(c.begin(), it, [](int i) {cout << i << endl; });
	//3,6,7
		
}

lambda expression

Summary :

  • The two sets to be intersection must be ordered sequences (must be ordered, otherwise there will be errors)When finding the intersection (and the difference of the union), it is actually easy to think of the need to sort. If you write it yourself, only sorting will speed up the time complexity.
  • To open up the space of the target container, you need to find the minimum value from the two containers (not necessary, the purpose is to reduce the waste of space)
  • The return value of set_intersection is an iterator of the last element in the intersection
set_union

Function description :

  • Find the union of two sets

Function prototype:

set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

// end1 container 1 end iterator
// beg2 container 2 start iterator
// end2 container 2 end iterator
// dest target container start iterator

#include"head.h"


int main()
{
	vector<int>a = { 1,2,3,4,5,6 };
	vector<int>b = {0,1,3,5,6,9 };
	vector<int>c(60);

	auto it=set_union(a.begin(), a.end(), b.begin(), b.end(), c.begin());//事实证明必须要排序,否则一定会出错,错误提示无效参数的传递,不过上述数组有序,所以不用排序;
	for_each(c.begin(), it, [](int i) {cout << i << " "; });
		
}

to sum up

  • The two sets of the union must be an ordered sequence
  • To open up space for the target container, two containers need to be added (not necessary, to save space)
  • Iterator of the last element in the union when set_union returns
set_difference

Function description:

  • Find the difference of two sets

Function prototype:

set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

// Note: The two sets must be an ordered sequence

// beg1 container 1 start iterator
// end1 container 1 end iterator
// beg2 container 2 start iterator
// end2 container 2 end iterator
// dest target container start iterator

#include"head.h"


int main()
{
	vector<int>a = {1,7,5,3,6 };
	vector<int>b = {4,7,5,3 };
	vector<int>c(60);
	sort(a.begin(), a.end(), [](int a, int b) {return a < b; });//一定要排序
	sort(b.begin(), b.end(), [](int a, int b) {return a < b; });
	auto it=set_difference(a.begin(), a.end(), b.begin(), b.end(), c.begin());//相当于数学中集合a-b;
	for_each(c.begin(), it, [](int i) {cout << i << endl; });
}

**to sum up**:

  • The two sets of the difference set must be an ordered sequence
  • The target container needs to take a larger value from the two containers to open up space (not necessary, just to reduce the waste of space)
  • The return value of set_difference is an iterator of the position of the last element in the difference set;

Guess you like

Origin blog.csdn.net/weixin_45929885/article/details/114240585