c++ STL set

/ * The SET
the SET and multiset elements will be sorted according to specific sorting principle.
In that the difference between the two, allowing multisetS repeated elements, and the set must be unique.
Common operations
set <int> s definition of a set of type int type container
s.begin () returns a pointer to the first element of the iterator
s.clear () clear all elements
s.count () Returns the value of a number of elements
s.empty () if the collection is empty, returns true
iterator s.end () returns a pointer to the last element after, not the last element
s.erase () to delete elements in the collection
s.find () returns a pointer to be find the iterator element, if not found returns end ()
s.insert () to insert elements in the collection
s.size () the number of elements in the set
s.swap () exchange two sets of variables
例子:
#include <iostream>
#include <set>
using namespace std;
int main()
{
    int i;
    set<int> set1;
    for(i=0; i<10; ++i)
        set1.insert(i);
    set<int>::iterator it;
    for(it=set1.begin(); it!=set1.end(); it++)
        cout<<*it<<"\t";
    cout<<endl;
    set1.erase(5);
    if(set1.insert(3).second)//把3插入到set1中,插入成功则set1.insert(3).second返回1,否则返回0.
        cout<<"set insert success";
    else
        cout<<"set insert failed";
    cout<<endl;
    set<int>::iterator itr;
    for(itr=set1.begin(); itr!=set1.end(); itr++)
        cout<<*itr<<"\t";
    set1.clear();
    return 0;
}

Guess you like

Origin www.cnblogs.com/QingyuYYYYY/p/11620918.html