Summarizes some of the common statements of set, which is partly referenced in this article: http://blog.163.com/jackie_howe/blog/static/199491347201231691525484/
#include <set>
realizes the red black tree's balanced binary search tree data structure, when inserting the element, it automatically adjusts the binary tree arrangement, puts the element in the appropriate position, guarantees each sub-tree root node key value is bigger than the left subtree all section
The key value of the point is less than the key value of all nodes in the right subtree, and the height of the left subtree of the root node is equal to the height of the right sub-tree.
The balanced binary search tree uses the middle sequence traversal algorithm, the retrieval efficiency is higher than the vector, the deque and the list and so on, in addition uses the middle sequence traversal can the key value to traverse from small to large.
The main purpose of constructing set sets is to retrieve quickly and not to modify key values directly.
Set <int> p;//collection container, no duplicate elements
Multiset <int> p;//Collection container with repeating elements
P.insert (k);//Add K to the set P
P.size ();//Returns the number of elements
P.empty ();//Determine if it is empty
P.clear ();//Clear all elements
P.erase (k);//delete the element K in the collection
P.count (K);//Returns the number of elements of a value (only valid for Multiset, and the count () value of set is only 0 and 1)
Set<int>::iterator it;//Defining iterators
Set<int>::reverse_iterator it;//defining a reverse iterator
It=p.begin ();//Pointing head
It=p.end ();//point to end, end contains no element
It=rbegin ()//Returns a reverse iterator that points to the last element in the collection
It=rend ()//Returns a reverse iterator that points to the first element in the collection
It=p.find (K);//Returns an iterator that points to the element being found, if no return P.end () is located;
Forward traversal
for (set<int>::iterator it=p.begin (); It!=p.end (); it++) {cout<<*it<< Endl;} // output elements, sorted by default in ascending order
/* Note that the iterator pointer cannot be arithmetic, only self-increment */
/*set Insert, find, delete operation complexity is O (log (N)) */
Other infrequently used functions:
Equal_range () returns two iterators in the collection with the upper and lower bounds equal to the given value
Get_allocator () returns the allocator of the collection
Lower_bound () returns an iterator that points to the first element greater than (or equal to) a value
Key_comp () returns a function for comparing values between elements
Max_size () returns the maximum limit of the elements that the collection can hold
Swap () swap two set variables
Upper_bound () returns an iterator that is greater than a value element
Value_comp () returns a function to compare the values between elements
Custom comparison functions
(1) The element is not a struct:
// Custom comparison function MyComp, overloaded "()" operator struct mycomp{ booloperator() (const your_type &a,const Your_type &B) { return a.data-b.data>0; }} set<int,mycomp>s; .. set<int, mycomp>::iterator it;
(2) If the element is a struct, the comparison function can be written directly in the structure body.
struct info{ string name; float score; // overloaded < "operator, custom collation bool operator < (const Info &a) const { // Sort by score from large to small return a.score& Lt score; }} set <info> S; .... set <info>::iterator it;
C + + STL set collection container