Set Collection Container

Source: Internet
Author: User

Recently learned the use of the set in the STL, here write a little summary and some of their own understanding.

The Set collection container implements the data structure of the balanced binary search tree of the red and black tree (red-black trees), and when the element is inserted, it automatically adjusts the arrangement of the binary tree, placing the element in the appropriate position to ensure that the key value of each subtree node is greater than the key value of all nodes of the left subtree. and the key value of all the nodes of the small less right operand subtree; Also, make sure that the height of the left subtree of the root node is equal to the height of the number of words, so that the height of the binary tree is the smallest and thus the fastest retrieval speed. It is important to note that it does not repeatedly insert elements of the same key value and takes ignore processing.

The retrieval of the balanced binary search tree using the sequential traversal algorithm, the retrieval efficiency is higher than that of the vectors, deque, and list containers. In addition, the middle sequence traversal algorithm can be used to traverse the key values from small to large, so it can be understood as the balanced binary search tree when inserting elements, the element key values are automatically ordered from small to large.

So now someone is going to ask, why is the insertion, deletion, and retrieval efficiency of set higher than other sequence containers?

First, we need to understand how set is stored, and the red-black tree (also known as the RB Tree) inside the set is a very efficient binary tree for balanced retrieval. Based on the red-black tree, all the elements within the set container are stored as nodes, and the node structure is similar to that of the linked list, pointing to the parent and child nodes. The structure chart might look like this:

A
/ \
B C
/ \ / \
D E F G

As a result, you just need to make a little transformation, and point the pointer to the new node. Delete the same time, a little change after the pointer to the node to point to the other node is OK. All that is done here is that the pointer is swapped out, and the memory movement is not related.

So what happens to the insertion, deletion, and search speed of set when the number of data elements increases?

If you know log2 's relationship, you should have a thorough understanding of the answer. Finding in set is using binary lookup, that is, if there are 16 elements, you will need to compare up to 4 times to find the result, 32 elements, and a maximum of 5 times. So there are 10,000 of them? The maximum number of comparisons is log10000, up to 14, and 20,000 if it is a single element. Up to 15 times. See, when the amount of data increases by one time, the number of searches is only 1 more times, more than 1/14 of the search time. Once you understand this, you can safely put the elements inside.

Before using set, you need to include the declaration "#include <set>" in the program header file.

Below we list some of the common operations in set: (English good small partners can go to this link to see, contains the set of almost all operations)

The first is to create a set collection object:

1 #include <set>23usingnamespace  std; 4 5 int Main () {6     set<int> s; //  7     return0; 8 }

element insertion and middle-order traversal:

Insert the element into the collection using the inset () method, where the insertion rule is inserted by the default comparison rule, by the element value from small to large, or by a custom comparison rule function If you specify a comparison rule function. Using a forward iterator to iterate through the collection, the result is exactly the result of ordering the elements.

1#include <iostream>2#include <Set>3 using namespacestd;4 5 intMain () {6     Set<int>s;7S.insert (8);//Five elements were inserted, but because a second insertion of 8 had duplicates, the second insertion of 8 did not perform8S.insert (1);9S.insert ( A);TenS.insert (6); OneS.insert (8); A  -     Set<int>::iterator it;//to define a forward iterator -      for(It=s.begin (); It!=s.end (); it++) {//The middle sequence iterates through all the elements in the collection thecout<<*it<<" "; -     } -cout<<Endl; -     return 0; +}

Operation Result:

1 6 8 12

We can also use the reverse iterator reverse_iterator to iterate through the collection, which is exactly the reverse sort result of the collection element. It needs to use the Rbegin () and the Rend () two methods, which give the starting and ending positions of the reverse traversal respectively.

1#include <iostream>2#include <Set>3 using namespacestd;4 5 intMain () {6     Set<int>s;7S.insert (8);//Five elements were inserted, but because a second insertion of 8 had duplicates, the second insertion of 8 did not perform8S.insert (1);9S.insert ( A);TenS.insert (6); OneS.insert (8); A      -     Set<int>::reverse_iterator it;//defining a reverse iterator -      for(It=s.rbegin (); It!=rend (); it++) {//Reverse Traversal thecout<<*it<<" "; -     } -cout<<Endl; -     return 0; +}

Run results

12 8 6 1

Deletion of elements:

Elements can use erase () to delete elements of a key value, or clear () to empty the collection.

Tomorrow more, haha

Set Collection Container

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.