Set is a set, although there are also key and real values, but the two are the same value, the key value is set to meet the underlying structure of the red and black tree, set operation and map very much like but also a little different.
1. The set iterator differs from map:
(1) Set uses the reference operator * to take a value, while map uses first and second values.
(2) A set iterator is a const iterator that cannot be used to modify the element being pointed to, whereas a map iterator can modify the pointing element.
2, set does not overload the [] operator, and the map is overloaded, because directly using [] change the value of the element will upset the original correct order, to change the element value must first delete the old element, then insert the new element
3, the type of construction is different, such as the same real value of the int type is set<int> and map<type,int> (where type can be any type)
Other operations such as inserting, deleting, and finding are almost the same as map
1#include <iostream>2#include <Set>3 using namespacestd;4 voidPrintSet<int>set_int)5 {6 Set<int>::iterator it=Set_int.begin ();7 for(; It!=set_int.end (); it++)8 {9cout<<*it<<' ';Ten } Onecout<<Endl; A - } - intMain () the { - intnum[4]={1,2,3,4}; - Set<int> Set_int (num,num+4); - + //element Insertion: - //1, insert value, return pair pair object, can judge whether insert succeeds according to. second. (Hint: value cannot be duplicated within the set container) + //Pair<iterator, bool> Insert (value) A //2, insert value before the POS position, return the new element position, but not necessarily be able to insert the success at //iterator Insert (&pos, value) - //3, insert all elements in the iteration interval [&first, &last] into the set container - //void Insert[&first, &last) -Set_int.insert (5); - print (set_int); -Set_int.insert (5); in print (set_int); - to //element Deletion + //1,size_type Erase (value) removes all elements in the set container that have the value of the element, returning the number of elements to be removed - //2,void Erase (&pos) removes the element at POS position, no return value the //3,void Erase (&first, &last) removes elements from the iteration interval [&first, &last], no return value * //4,void Clear () to remove all elements within the set container $Set_int.erase (3);Panax Notoginseng print (set_int); - the //Element Lookup + //count (value) returns the number of elements within the set object that have the value of the element A //iterator Find (value) returns the location of value where value will be returned to end () the //Lower_bound (value), Upper_bound (value), Equal_range (value) + Set<int>:: iterator it; -It=set_int.find (5); $cout<<*it<<Endl; $ return 0; -}
Basic operation of C + + STL set