Set Collection container: 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 node's key value, is less than the right subtree all node's key value; In addition, Also ensure that 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. It is important to note that it does not repeatedly insert the same key value, but takes ignoring processing.
Set sets are primarily issues such as handling collections
Viii. Set Set Container
1. Create a Set Collection object
Set<int> S;
2. Inserting elements
S.insert (2);
3. Deleting an element
S.erase (2);
4. Find elements
Set<int>::iterator it;
Successful return of element iterator position, failure to return end () iterator position
It=s.find (2);
5. Custom comparison functions
(1) The element is not a structural body
struct COMP
{
BOOL Operator () (const int &A,CONST int &b)
{
In order from large to small
returna>b;
}
};
Set<int,comp> S;
(2) elements are structural bodies
struct INFO
{
Float score;
String Name;
BOOL operator < (const Info &i) const
{
Sort by score from large to small
Return i.score<score;
}
};
Set<info> S;
Map, as long as it is to deal with the problem of one by one correspondence, such as the monotone increment function is one of the corresponding relationship, because each x has and only one y corresponds to it
X. Map Mapping Container
1. Create a Map object
Map<int,char> M;
2. Deleting an element
M.erase (2);
3. Find elements
Map<int,char>::iterator it;
The position of the iterator where the key value was successfully returned, and the end () iterator position failed to return
It=m.find (2);
4. Custom comparison functions
(1) The element is not a structural body
struct COMP
{
BOOL Operator () (const int &A,CONST int &b)
{
Key values from large to small
returna>b;
}
};
Map<int,char,comp> M;
(2) elements are structural bodies
struct INFO
{
Float score;
String Name;
BOOL operator < (const Info &i) const
{
Sort by score from large to small
Return i.score<score;
}
};
Map<info,int> M;
Set and Map containers,