[STL] set operations

There may be implemented STL intersection, union, difference, symmetric difference algorithm set.

Before use you need to include the header file:

#include <algorithm>

Note: Use compute the intersection and union algorithms must ensure that the operation involved two sets ordered! ! !

Intersection:

Example: find {1,2,3} and {2,3,4} the intersection:

We need to use the function:

set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));

The first parameter and the second parameter is in the form of iterator specifies a range of the first set involved in computing.

The third parameter and the fourth parameter is also in the form of iterators, the second range specifies the set involved in computing.

The fifth parameter is inserted iterator, it contains two parameters, a first set of parameter specifies the calculated result is stored to the second parameter is in the form of an iterator, specifies a corresponding set of results which is inserted before position.

Calculation and back union, difference, intersection and calculation functions are the same symmetric difference function usage details are not repeated.

Code:

#include <bits/stdc++.h>
#define re register
#define e endl 
using namespace std;
const int N = 100005;
vector<int> v1, v2, v3;
inline int read(){
	int f = 0, x = 0; char ch;
	do {ch = getchar(); f |= ch == '-';} while (!isdigit(ch));
	do {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();} while (isdigit(ch));
	return f ? -x : x;
}
int main(){
	for (re int i = 1; i <= 3; ++i)v1.push_back(i);//v1 1 2 3
	for (re int i = 2; i <= 4; ++i)v2.push_back(i);//v2 2 3 4
	set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));//v3 2 3
	for (re int i = 0; i < v3.size(); ++i)cout << v3[i] << '';
	return 0;
}

Union:

Example: find {1,2,3} and {2,3,4} and the sets:

We need to use the function:

set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));

Code:

#include <bits/stdc++.h>
#define re register
#define e endl 
using namespace std;
const int N = 100005;
vector<int> v1, v2, v3;
inline int read(){
	int f = 0, x = 0; char ch;
	do {ch = getchar(); f |= ch == '-';} while (!isdigit(ch));
	do {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();} while (isdigit(ch));
	return f ? -x : x;
}
int main(){
	for (re int i = 1; i <= 3; ++i)v1.push_back(i);//v1 1 2 3
	for (re int i = 2; i <= 4; ++i)v2.push_back(i);//v2 2 3 4
	set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));//v3 1 2 3 4
	for (re int i = 0; i < v3.size(); ++i)cout << v3[i] << ' ';
	return 0;
}

Difference set:

Example: find {1,2,3} and {2,3,4} the set difference:

We need to use the function:

set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));

Code:

#include <bits/stdc++.h>
#define re register
#define e endl 
using namespace std;
const int N = 100005;
vector<int> v1, v2, v3;
inline int read(){
	int f = 0, x = 0; char ch;
	do {ch = getchar(); f |= ch == '-';} while (!isdigit(ch));
	do {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();} while (isdigit(ch));
	return f ? -x : x;
}
int main(){
	for (re int i = 1; i <= 3; ++i)v1.push_back(i);//v1 1 2 3
	for (re int i = 2; i <= 4; ++i)v2.push_back(i);//v2 2 3 4
	set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));//v3 1
	for (re int i = 0; i < v3.size(); ++i)cout << v3[i] << ' ';
	return 0;
}

Symmetric difference:

Example: find {1,2,3} and {2,3,4} the symmetric difference:

We need to use the function:

set_symmetric_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));

Code:

#include <bits/stdc++.h>
#define re register
#define e endl 
using namespace std;
const int N = 100005;
vector<int> v1, v2, v3;
inline int read(){
	int f = 0, x = 0; char ch;
	do {ch = getchar(); f |= ch == '-';} while (!isdigit(ch));
	do {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();} while (isdigit(ch));
	return f ? -x : x;
}
int main(){
	for (re int i = 1; i <= 3; ++i)v1.push_back(i);//v1 1 2 3
	for (re int i = 2; i <= 4; ++i)v2.push_back(i);//v2 2 3 4
	set_symmetric_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));//v3 1 4
	for (re int i = 0; i < v3.size(); ++i)cout << v3[i] << ' ';
	return 0;
}

Guess you like

Origin www.cnblogs.com/luotei/p/11109360.html