set container of C++STL

Keywords: C++

Set is a kind of Standard Association container (vector,list,string,deque are all sequence containers, while set, multiset, map, multimap are standard association containers) in STL. Its bottom layer is realized by using a balanced search tree red black tree. When inserting and deleting operations, only pointer operation nodes are needed, and memory movement and copy are not involved, so the efficiency is relatively high. Set, as the name implies, means "set". In a set, the elements are unique. By default, the elements are automatically arranged in ascending order. It supports some operations on the set, such as set  intersection  difference and set  union  symmetry  difference. If the elements in the set are allowed to be repeated, multiset can be used.

#include<set>
#include<iterator>
#include<iostream>
using namespace std;
int main()
{
set<int>v1;

//insert insert
v1.insert(1);
v1.insert(6);
v1.insert(5);
v1.insert(1);  //Element 1 because it already exists set 1 will not be inserted again in
v1.insert(10);
v1.insert(9);
//ergodic set,It can be found that the elements are ordered set<int>::iterator it; for(it=v1.begin();it!=v1.end();it++)   cout<<*it<<" "; cout<<endl; //Use size()Function to get the current number of elements cout<<v1.size()<<endl; if(v1.find(200)==v1.end()) //find()Function to find if an element exists cout<<"200 isn't in the set v1"<<endl; //find We'll find them one by one set,
//When set.end() is reached, none is found, and end() is returned
set<int>v2; for(int i=6;i<15;i++) v2.insert(i); for(it=v2.begin();it!=v2.end();it++) cout<<*it<<" "; cout<<endl; set<int>v3; //Get two set And set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),insert_iterator<set<int> >(v3,v3.begin()));//Note the form of the fifth parameter copy(v3.begin(),v3.end(),ostream_iterator<int>(cout," ")); cout<<endl; v3.clear();//Note that the set To invoke clear()Function clear //Get two set Intersection set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),insert_iterator<set<int> >(v3,v3.begin())); copy(v3.begin(),v3.end(),ostream_iterator<int>(cout," ")); cout<<endl;
//Get two set Difference v3.clear(); set_difference(v1.begin(),v1.end(),v2.begin(),v2.end(),insert_iterator<set<int> >(v3,v3.begin())); copy(v3.begin(),v3.end(),ostream_iterator<int>(cout," ")); cout<<endl;
//Get two set That is to say, two sets are assumed to be A and B So the symmetry difference is AUB-A∩B v3.clear(); set_symmetric_difference(v1.begin(),v1.end(),v2.begin(),v2.end(),insert_iterator<set<int> >(v3,v3.begin())); copy(v3.begin(),v3.end(),ostream_iterator<int>(cout," ")); cout<<endl; return 0; }

Posted by mickey9801 on Tue, 31 Mar 2020 06:28:19 -0700