STL containers usage and set codeforces 685B

How not previously used set, then hang training game, it was found the magical set, combined with online usage while learning while writing.

First set is a container, like other STL containers can be defined by the set <int> s, which is included in the STL file header #include <set> in. 

Its interior is a red-black tree to achieve, is to ensure that a set which is ordered, the default sort is ascending order, and set the container elements are unique, if it is repeated inside the element allows use multiset container to achieve.

1. set of common operations

s.begin () returns the position of the first element of the set container (first iterator) 

s.end () Returns a set position of the last element of the container (last iterator) 

s.clear () set to empty the container 

s.empty () determines whether the set is empty container 

s.insert () insert an item 

delete an element s.erase () 

s.size () returns the number of elements inside the container 

s.max_size () returns a set of containers may contain The maximum number of elements

Example: 

#include <bits / STDC ++ H.> The using namespace STD; typedef Long Long LL; SET < int > S; int main ( int argc, char * the argv []) { s.clear (); IF (s.empty ()) << COUT " tank empty " << endl; the else COUT << " container is not empty " << endl; s.insert ( . 3 ); s.insert ( . 6 ); s.insert ( . 1 ) ; COUT<< " set to the first value: " << * s.begin () << endl; COUT << " last set of values: " << * s.end () << endl; COUT < < " MAXSIZE value of the set: " << s.max_size () << endl; return 0 ; }

operation result:

 

 

 

2. set the count () function

count () is used to find the set number of times a value occurs, but due to repeated elements in this set appear not allowed, so the set with s.count () will return either 0 or 1, can be used to determine the need to find the number of it exists or not in the set , while you can count the number of multiset in a number appeared.

实例:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
set<int> s1;
multiset<int> s2;
int main(int argc, char * argv[]){
       s1.insert(3);
       s1.insert(6);
       s1.insert(1);
       s1.insert(1);
       s2.insert(3);
       s2.insert(6);
       s2.insert(1);
       s2.insert(1);
       cout<<"set中1出现的次数:"<<s1.count(1)<<endl;
       cout<<"set中2出现的次数:"<<s1.count(2)<<endl;
       cout<<"multiset中1出现的次数:"<<s2.count(1)<<endl;
       cout<<"multiset中2出现的次数:"<<s2.count(2)<<endl;
    return 0;
}

运行结果:

 

 

3.删除元素

s.earse(iterator)      删除iterator指向的值

s.earse(a,b)            删除定位器a和b之间的值

s.earse(key_value)      删除键值key_value的值

实例:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
set<int> s;
int main(int argc, char * argv[]){
    set<int>::iterator it;
       set<int>::iterator a;
       set<int>::iterator b;
       for(int i=1;i<=10;i++){
           s.insert(i);
       }
       cout<<"删除之前set中的元素是:"<<endl;
       for(it=s.begin();it!=s.end();it++){
           cout<<*it<<" ";
       }
       cout<<endl;
       s.erase(s.begin()); //删除指向的值
       cout<<"第一次删除之后set中的元素是:"<<endl;
       for(it=s.begin();it!=s.end();it++){
           cout<<*it<<" ";
       }
       cout<<endl;
       a=s.begin();
       cout<<"a指向的值是:"<<*a<<endl;
       b=s.begin();
       b++,b++,b++;
       cout<<"b指向的值是:"<<*b<<endl;
       s.erase(a,b); //删除(a,b)之间的值,a和b都是迭代器
       cout<<"第二次删除之后set中的元素是:"<<endl;
       for(it=s.begin();it!=s.end();it++){
           cout<<*it<<" ";
       }
       cout<<endl;
       s.erase(9); //删除值
       cout<<"第三次删除之后set中的元素是:"<<endl;
       for(it=s.begin();it!=s.end();it++){
           cout<<*it<<" ";
       }
       cout<<endl;
    return 0;
}

运行结果:

 

可以发现用第二种方法删除的值是 从a到b, 这里是包含a指向的元素而不包含b指向的元素, 就是 [a,b). 同时set中的删除操作是不会进行错误检查的, 比如定位器是否合法等等,所以用的时候要注意.

 

4.查找元素

前面说过用count()可以进行元素的查找, 还有一个函数find()也可以进行元素查找

count()可以查找元素存不存在,而find()可以找到元素的位置, 但是如果没有找到就返回end().

实例:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
set<int> s;
int main(int argc, char * argv[]){
    set<int>::iterator it;
    for(int i=1;i<=10;i++){
        s.insert(i);
    }
    cout<<"容器中的元素有:"<<endl;
    for(it=s.begin();it!=s.end();it++){
        cout<<*it<<" ";
    }
    cout<<endl;
    int a;
    cout<<"请输入要查找的元素:"<<endl;
    while(cin>>a){
        if(s.find(a)==s.end()) cout<<"没有找到该元素"<<endl;
        else cout<<*s.find(a)<<endl;
    }
    return 0;
}

运行结果:

 

 

 5. s.lower_bound(key_value)    返回第一个大于等于key_value的定位器

  s.upper_bound(key_value)    返回第一个大于key_value的定位器

  这两个函数都是用二分来实现查找的,所以比较快.

  

  

Guess you like

Origin www.cnblogs.com/wushengyang/p/11934329.html