Create a Set Object
To manage the set of two-tree linked list data, a set object is created with the constructor of the set container.
(1) Set ()
Creates a set object without any data element with the default Less<t> function object and memory allocator.
Set<int> s; An empty set object S is created, and the element type is int integer;
(2) Set (const key_compare& COMP)
Specifies a comparison function object comp to create a set object, the memory allocator is the default value.
Defines a string comparison function object Strless
struct Strless {
BOOL Operatro () (const char *S1, const char *S2) const
{
Return strcmp (S1, S2) < 0;
}
};
Create a set Container object s
Set<const char*, strless> S (strless ());
(3) Set (const set&)
The set copy constructor, through the red black tree copy constructor function, realizes two sets container's element, the head node and the number of nodes copies.
set<int> S1;
set<int> S2 (S1);
(4) Set (Inputiterator first, Inputiterator last)
Creates a set object with the element referred to by the interval iterator [first, last].
int IArray = {13, 32,19};
set<int> s (IArray, iarray+3);
(5) Set (Inputiterator first, Inputiterator last, const key_compare& comp)
Create a set object with the element and Comp function object referred to by the interval iterator [first, last].
Const char* Szarray = {"Hello", "dog", "Bird"};
Set<const char*, strless> s (Szarray, szarray+3, strless ());
Insertion of elements
The set does not have a trailing Insert function push_back (), and the insertion of an element is typically inserted using insert for dynamic retrieval.
(1) Pair<iterator, bool> Insert (const value_type& v)
Inserting element V into the set container requires that the V value not be duplicated with any element of the set container, or the insert fails. Returns a pair pair object that provides the iterator position of the inserted element and the True/false Insert success flag.
(2) Iterator insert (iterator position, const value_type& v)
Insert the element V into the set container, the parameter position hint to insert v before the position position, the insertion position returned is determined by the actual situation, and may not be inserted before the position position.
(3) void Insert (Inputiterator fist, Inputiterator last)
Inserts the data referred to by an iterator interval [first, last] as an element into the set container.
Deletion of elements
As with insertions, the set container also has efficient removal of red-black tree elements and automatically adjusts the internal red-black tree balance.
(1) Void erase (iterator position)
Delete the element that position refers to
(2) Size_type Erase (const key_type& k)
Delete the element equal to the key value K, this function always returns a value of 1 for the set container, because the set container does not have duplicate element values (key values).
(3) Void erase (iterator first, iterator last)
Deletes all elements on the set iterator interval [first, last].
(4) void Clear ()
Removes all elements, but does not delete the head node of the inner red-black tree.
Traversal access of elements
The iterator to the set container provides access to the elements in the inner red-black tree, usually first using the begin and end functions to find the first and end elements of the traversal, and then from the iterator's "+ +" operation, the value of the element is small to large.
Iterator begin ();
Iterator End ();
Reverse traversal of elements
By using the direction iterator reverse_iterator and const_reverse_iterator defined by the set container, the inverse middle order traversal of the red and black tree can be realized, thus traversing the elements from large to small.
Reverse_iterator Rbegin ();
Reverse_iterator rend ();
Searching for elements
The Set container provides a function that applies a red-black tree to search for find, which returns an iterator value that is the location of the searched element, and returns the end element position if the element does not exist
Iterator Find (const key_type& k) const
Basic operation of Set:
Begin () returns the iterator that points to the first element
Clear () Clears all elements
COUNT () returns the number of elements of a value
Empty () returns True if the collection is null
End () returns an iterator pointing to the last element
Equal_range () returns two iterators in the collection with the upper and lower bounds equal to the given value
Erase () Delete elements in the collection
Find () returns an iterator that points to the element being found
Get_allocator () returns the allocator of the collection
Insert () Inserts an element into 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
Rbegin () returns a reverse iterator that points to the last element in the collection
Rend () returns a reverse iterator that points to the first element in the collection
The number of elements in the size () collection
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
5, custom comparison function:
1#include <iostream>2#include <Set>3 using namespacestd;4typedefstruct {5 intb;6 Chars;7 }newtype;8 structCompare//there is no ().9 {Ten BOOL operator()(ConstNewtype &a,ConstNewtype &b)Const One { A returna.s<B.S; - } -};//the ";" is here; the Set<newtype,compare>element; - intMain () - { - Newtype a,b,c,d,t; +A.a=1; a.s='b'; -B.a=2; b.s='C'; +C.a=4; c.s='D'; AD.a=3; d.s='a'; at Element.insert (a); - Element.insert (b); - Element.insert (c); - Element.insert (d); - Set<newtype,compare>:: iterator it; - for(It=element.begin (); It!=element.end (); it++) incout<< (*it) .a<<" "; -cout<<Endl; to for(It=element.begin (); It!=element.end (); it++) +cout<< (*it) .s<<" "; -}
6. Other set construction methods:
1#include <iostream>2#include <Set>3 using namespacestd;4 BOOLFncomp (intLhsintRHS) {returnlhs<RHS;}5 structClasscomp {6 BOOL operator() (Const int& LHS,Const int& RHS)Const7{returnlhs<RHS;}8 };9 intMain ()Ten { One Set<int> First;//empty set of INTs A intmyints[]= {Ten, -, -, +, -}; - Set<int> Second (myints,myints+5);//pointers used as iterators - Set<int> third (second);//a copy of second the Set<int> Fourth (Second.begin (), Second.end ());//iterator ctor. - Set<int,classcomp> Fifth;//class as Compare - BOOL(*FN_PT) (int,int) =Fncomp; - Set<int,BOOL(*) (int,int) > Sixth (FN_PT);//function pointer as Compare + return 0; -}
Std::set