I. Explanation
about set must be explained by set associative container. set As a container is also used to store data types of the same data type, and can extract data from a collection of data, in the set unique and the system can be automatically sorted according to the value of the element.
Two. Usage
1. Header files
#include <set
2. Common operation
set<int> s defines a set container type int type s.begin () returns an iterator to the first element s.clear () Clears all elements S.count () returns the number of elements of a value s.empty () If the collection is empty, return Trues.end () returns the iterator that points after the last element, not the last element s.erase () Removes an element from the collection S.find () returns an iterator to the element that was found, and returns end () S.insert () when it is not located () inserts an element in the collection s.size () the number of elements in the collection S.swap () swap two set variables
Three. Example:
#include <iostream>#include<Set>using namespacestd;intMain () {inti; Set<int>Set1; for(i=0; i<Ten; ++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)//Insert 3 into Set1, insert success Set1.insert (3). Second returns 1, otherwise returns 0.cout<<"Set Insert Success"; Elsecout<<"Set Insert Failed"; cout<<Endl; Set<int>:: Iterator ITR; for(Itr=set1.begin (); Itr!=set1.end (); itr++) cout<<*itr<<"\ t"; Set1.clear (); return 0;}
C + + STL set detailed