c++ STL set: Set

<set Collection >

set is an associated container of STL. Its elements will not be repeated and will be automatically presented in order.

Header file: include < set >

Function: it is used to store data types of the same data type, and can extract data from a data set. The value of each element in the set is unique, and the system can automatically sort according to the value of the element. The internal use is a very efficient balanced search Binary Tree: red black tree.

Common statement: begin() returns map header iterator

end() returns the tail iterator

clear() clears all elements

erase (elem) delete an element

Empty() returns true if it is empty

Size() returns the set size

count (elem) returns the number of elements

insert(elem) inserts an element

 

 

Take a simple example:

 /*
 qq:1239198605
 ctgu_yyf
        */

#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set> 
#define ll long long
using namespace std;

set<int> ans;
int main()
{
   ios::sync_with_stdio(false);
   
   int a[10]={1,2,1,3,4,5,9,9,9,6};
   for(int i=0;i<10;i++)
   ans.insert(a[i]);
   
   
   set<int>::iterator iter;
   //Elements in the output container 
   for(iter=ans.begin();iter!=ans.end();iter++)
   cout<<*iter<<" ";
   cout<<endl; 
   
   //Output the number of nine elements in easy 
   cout<<ans.count(9)<<endl;
   
   //Clear element 9 
   ans.erase(9);
   
    //Output the number of nine elements in easy 
   cout<<ans.count(9)<<endl;


return 0;
}

Output:

1 2 3 4 5 6 9
1
0

        

Example of Niuke A:

Links: https://www.nowcoder.com/acm/contest/132#question

 /*
 qq:1239198605
 ctgu_yyf
        */

#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set> 
#define ll long long
using namespace std;

set<int> ans;
int main()
{
   ios::sync_with_stdio(false);
   int k;
   int a[6],b[6]={0};
   
   for(int i=0;i<6;i++)
   cin>>a[i];
   
   for(int i=0;i<6;i++)
   for(int j=0;j<6;j++)
   if(a[i]==a[j])
   {
   	b[i]++;
   }
   
   for(int i=0;i<6;i++)
   ans.insert(b[i]);
   
   if((ans.count(4)||ans.count(5))&&ans.count(1))
   cout<<"Bear"<<endl;
   
   else if((ans.count(4)&&ans.count(2))||ans.count(6))
   cout<<"Elephant"<<endl;
   
   else 
   cout<<"Hernia"<<endl;
   


return 0;
}

 

Tags: iOS

Posted on Sun, 12 Jan 2020 11:31:08 -0500 by asgsoft