Set and Map containers,

Source: Internet
Author: User
Tags set set

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,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.