Use of C++ set
- 2020-05-30 20:55:38
- OfStack
Use of C++ set
set is also a common container in STL. The set collection container implements a balanced two-fork retrieval tree of red-black trees, which automatically adjusts the arrangement of the two-fork tree to put the elements in place. The set container contains elements whose values are unique to one, and the elements in the collection are arranged in order of one.
We constructed the set collection for quick retrieval, not to modify the key directly.
Some common operations of set:
Most of set's operations are similar to vector, except that set does not support random access and must be accessed using an iterator. There is only one insert insert in set, since putting one element into set will adjust the position of the element and put it into the appropriate position.
For a set, we have union, intersection, difference, complement, etc., so we have similar set operations in set, which are all in #include
<
algorithm
>
Under the header file:
std::set_intersection() : The function is to find the intersection of two sets.
std::set_union() : Take the union of two sets
std::set_difference () : difference set
std::set_symmetric_difference () : the result is The first 1 The number of iterators relative to the number 2 A set of difference And on the first 2 One relative to number one 1 A set of difference
There is a question on school OJ to do these operations. The following is the question on school OJ:
Description
The operation of a set is to specify a new set with a given set. set A and B Is a set, then their union difference intersection complement is defined as follows:
A ∪ B={x|x ∈ A ∨ x ∈ B}
A studying B={x|x ∈ A Sunday afternoon x ∈ B}
A-B={x|x ∈ A Sunday afternoon x Do not belong to B}
SA ={x|x ∈ (A ∪ B) Sunday afternoon x Do not belong to A}
SB ={x|x ∈ (A ∪ B) Sunday afternoon x Do not belong to B}
Input
The first 1 Line of input 1 A positive integer T So there's a total of T Group test data. ( T<=200 )
And then down here 2T Each line, 1 Lines have n+1 Number one, number two 1 A number is n(0<=n<=100) That means there's more after that bank n Number input.
Output
For each set of test data, first output the test data sequence number," Case #.NO ",
And then output 7 All right, every line 1 A collection of
before 2 Rows output collections separately A , B , take 5 Row to output the collection separately A , B And of the (A u B) , (A n B) Poor, (A � B) And repair.
The elements in the set are called" {} "Expand, and use between elements," "Separated.
Sample Input
14 1 2 3 10
Sample Output
Case# 1:
A = {1, 2, 3}
B = {}
A u B = {1, 2, 3}
A n B = {}
A - B = {1, 2, 3}
SA = {}
SB = {1, 2, 3}
My code is as follows:
#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
using namespace std;
void print(set<int> a)
{
if(a.begin() == a.end())
cout << "}" << endl;
for(set<int>::iterator it = a.begin();it!=a.end();it++)
{
if(++it==a.end())
{
it--;
cout << *it << "}\n";
}
else
{
it--;
cout << *it << ", ";
}
}
}
int main()
{
int T, cou = 0;
set<int> a, b, c;
cin >> T;
while(T--)
{
cou++;
a.clear(), b.clear(), c.clear();
int n;
cin >> n;
for(int i=0;i<n;i++)
{
int x;
cin >> x;
a.insert(x);
}
cin >> n;
for(int i=0;i<n;i++)
{
int x;
cin >> x;
b.insert(x);
}
cout << "Case# " << cou << ":" << endl;
cout << "A = {";
print(a);
cout << "B = {";
print(b);
set_union(a.begin(),a.end(),b.begin(),b.end(),inserter(c,c.begin()));
cout << "A u B = {";
print(c);
c.clear();
set_intersection(a.begin(),a.end(),b.begin(),b.end(),inserter(c,c.begin()));
cout << "A n B = {";
print(c);
c.clear();
set_difference(a.begin(),a.end(),b.begin(),b.end(),inserter(c,c.begin()));
cout << "A - B = {";
print(c);
c.clear();
set_difference(b.begin(),b.end(),a.begin(),a.end(),inserter(c,c.begin()));
cout << "SA = {";
print(c);
c.clear();
set_difference(a.begin(),a.end(),b.begin(),b.end(),inserter(c,c.begin()));
cout << "SB = {";
print(c);
}
return 0;
}
inserter is an insert iterator in an iterator adapter. Principle: it calls insert() internally
Function: inserts an element at a specified location in a container
Limitation: inserter can only be used in containers that provide the inset() member function. All STL containers provide the inset() function.
Applicable: all STL containers
If you have any questions, please leave a message or come to the site community to exchange discussion, thank you for reading, hope to help you, thank you for your support of the site!