Usage of C ++ set
Set is also a common container in STL. The set collection container realizes the balanced binary search tree data structure of the red and black trees. It automatically adjusts the arrangement of Binary Trees and places the elements in the appropriate positions. The values of the elements contained in the set container are unique, and the elements in the set are arranged in a certain order.
The purpose of constructing the set is to quickly retrieve and cannot directly modify the key value.
Some common operations of set:
Begin () returns the iterator pointing to the first element.
Clear () clear all elements
Count () returns the number of elements of a value.
Empty () returns true (true) if the set is null)
End () returns the iterator pointing to the last element, not the last element.
Erase () deletes elements in a set.
Find () returns an iterator pointing to the element to be found.
Insert () inserts an element in the set.
Max_size () returns the maximum value of elements that can be accommodated in a set.
Number of elements in the size () Set
Swap () exchange two set variables
In fact, most of set operations are similar to vector operations. However, set does not support random access and must be accessed using an iterator. Since the position of an element is adjusted when set is put into an appropriate position, there is only one insert operation in the set.
For a set, we generally have operations such as Union, intersection, difference set, and supplement set. Therefore, we also have similar set operations in the set operation, they are all under the # include header file:
Std: set_intersection (): This function is used to calculate the intersection of two sets. Std: set_union (): returns the Union set of the Two Sets std: set_difference (): difference set std: set_1_ric_difference (): the result is that the first iterator has a difference set relative to the second iterator and the first iterator has a difference set relative to the first iterator.
The school OJ has a question to perform these operations. The following is the question of the school OJ:
The Description set operation specifies a new set with the given set. If A and B are set, their Union Difference makeup sets are defined as follows: A records B = {x | x records A records x records B} A-B = {x | x records A records x not B} SA = {x | x ε (A ∪ B) when x does not belong to A} SB = {x | x ε (A then B) when x does not belong to B} Input, the first line is Input A positive integer T, indicates a total of T groups of test data. (T <= 200) then there are 2 T rows below, each row has n + 1 digits, where the first digit is n (0 <= n <= 100 ), it indicates that there are n numbers after the row. Output for each group of test data, first Output the test data sequence number, "Case #. "NO". The next output contains seven rows. Each row is A set. The first two rows Output A and B respectively, the next five rows output the sum (A u B), intersection (A n B), difference (A-B), and population of the Set A and B respectively. The elements in the set are expanded with "{}" and separated. Sample Input14 1 2 3 10 Sample OutputCase #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
#include
#include#include
using namespace std;void print(set
a){ if(a.begin() == a.end()) cout << "}" << endl; for(set
::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
a, b, c; cin >> T; while(T--) { cou++; a.clear(), b.clear(), c.clear(); int n; cin >> n; for(int i=0;i
> x; a.insert(x); } cin >> n; for(int i=0;i
> 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 the insert iterator in the iterator adapter.
Principle: it calls insert () internally ()
Function: insert an element at the specified position of the container.
Restriction: inserter can be used only in containers that provide inset () member functions. All STL containers provide inset () functions.
Applicable: All STL containers