C + + Primer (Fifth edition) learning Note _6_ Standard Template Library _set collection container

Source: Internet
Author: User
Tags set set

C + + Primer (Fifth edition) learning Note _6_ Standard Template Library _set collection container

The Set collection container implements the data structure of the balanced binary search tree of the red and Black Tree (Red-blacktree), and when the element is inserted, it automatically adjusts the sorting of the binary tree and places the element in the appropriate position.

(1) Ensure that the key value of each sub-tree root node is greater than the key value of all nodes of the left subtree, and the key value of all nodes of the small less right operand subtree;

(2) Also, make sure that the height of the left subtree of the root node is equal to the height of the right sub-tree. In this way, the height of the binary tree is the smallest, thus the fastest retrieval speed.


The retrieval of the balanced binary search tree uses the sequential traversal algorithm, and the retrieval efficiency is high. By default, key values are traversed from small to large.

For the key values in the Set container, you cannot modify them directly. Because if a key value in the container is modified, the set container rotates the subtree according to the new key value, so that the modified key value is probably not in the original position.

1. Create set Set object, element insert and middle sequence traversal

Inserts the element into the collection by using the Insert () method, which, by default, inserts the key value from small to large.

#include <iostream> #include <stdio.h> #include <vector> #include <set> using namespace std; int main () {   set<int> str;   5 elements were inserted, but since 8 had duplicates, the second insert of 8 did not perform   Str.insert (8);//first insertion of 8, can be inserted into   str.insert (1);   Str.insert (n);   Str.insert (6);   Str.insert (8); The second insertion of 8 does not insert    //In the sequence iterates through all the elements in the collection for   (set<int>::iterator iter = Str.begin (); iter!= str.end (); iter++)       cout << *iter << "";   cout << Endl;   return 0;}

Operation Result:

1 6 8 12

2. Reverse traversal of elements

Use the reverse iterator reverse_iterator to traverse the collection in reverse. It needs to use the Rbegin () and Rend () two methods, giving the start and end positions of the reverse traversal, respectively.

#include <iostream> #include <stdio.h> #include <vector> #include <set> using namespace std; int main () {   set<int> str;   5 elements were inserted, but since 8 had duplicates, the second insert of 8 did not perform   Str.insert (8);//first insertion of 8, can be inserted into   str.insert (1);   Str.insert (n);   Str.insert (6);   Str.insert (8); The second insertion of 8 does not insert    //In the sequence to traverse all elements in the collection for   (set<int>::reverse_iterator iter = Str.rbegin (); Iter!=str.rend (); iter++)       cout << *iter << "";   cout << Endl;   return 0;}

Operation Result:

12 8 6 1

3. Deletion of elements

Set set has an efficient delete processing function and automatically adjusts the balance of the inner red and black tree.

Delete the element of a key value with the erase () method, emptying the collection with the Clear () method.

#include <iostream> #include <stdio.h> #include <vector> #include <set> using namespace std; int main () {    set<int> str;    5 elements were inserted, but since 8 had duplicates, the second insert of 8 did not perform    Str.insert (8);//first insertion of 8, can be inserted into    str.insert (1);    Str.insert (n);    Str.insert (6);    Str.insert (8); The second insertion of 8 does not insert     //delete the element with a key value of 6    str.erase (6);     The middle sequence iterates through all the elements in the collection for    (set<int>::iterator iter = Str.begin (); Iter!=str.end (); iter++)        cout << * ITER << "";    cout << Endl;     Empty set    str.clear ();    cout << str.size () << Endl;    return 0;}

Operation Result:

8 12

0

4. Retrieval of elements

Searches the collection using the Find () method, and returns the position of the key value iterator if the key value is found. Otherwise, returns end ().

#include <iostream> #include <stdio.h> #include <vector> #include <set> using namespace std; int main () {    set<int> str;    5 elements were inserted, but since 8 had duplicates, the second insert of 8 did not perform    Str.insert (8);//first insertion of 8, can be inserted into    str.insert (1);    Str.insert (n);    Str.insert (6);    Str.insert (8); Insert 8 for the second time, will not be inserted     set<int>::iterator iter = str.find (6);    if (iter! = Str.end ())       cout << "found value:" << *iter<< Endl;    else       cout << "not found" << Endl;       ITER = Str.find (+);    if (iter! = Str.end ())       cout << *iter <<endl;    else       cout << "not found" << Endl;    return 0;}

Operation Result:

The value found is: 6

Not found

5. Custom comparison function

By default, elements are inserted in the order of the key values from small to large. Because the internal data structures are red-black trees. There are two ways to write,

(1) If the element is not a struct, you can write a comparison function. The following implements the key values by inserting the elements into the set in the order of the Boulevard small:

#include <iostream> #include <stdio.h> #include <vector> #include <set> using namespace std; struct mycomp{    bool operator () (int a, int b)    {       return a > B;    }}; int main () {    set<int, Mycomp> ; STR;    5 elements were inserted, but since 8 had duplicates, the second insert of 8 did not perform    Str.insert (8);//first insertion of 8, can be inserted into    str.insert (1);    Str.insert (n);    Str.insert (6);    Str.insert (8); Insert 8 for the second time, will not insert for     (set<int, Mycomp>::iterator iter = Str.begin (); Iter!=str.end (); iter++)       cout < < *iter << "";    cout << Endl;    return 0;}

Operation Result:

12 8 6 1

(2) If the element is a struct, then it is possible to write the comparison function directly inside the structure body.

#include <iostream> #include <stdio.h> #include <vector> #include <set> using namespace std; struct info{   string name;   float score;   BOOL Operator < (Info a) const    {       //by score from large to small. If you want to arrange from small to large, use the ">" sign.       return A.score < score;    }}; int main () {   set<info> str;   Info info;    Info.name = "Jack";   Info.score = 80.5;   Str.insert (info);    Info.name = "Tomi";   Info.score = 20.5;   Str.insert (info);    Info.name = "Nacy";   Info.score = 60.5;   Str.insert (info);    for (Set<info>::iterator iter = Str.begin (); ITER! = Str.end (); iter++)       cout << (*iter). Name << ":" << (*iter). Score << Endl;   return 0;}


Operation Result:

jack:80.5

nacy:60.5

tomi:20.5

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + Primer (Fifth edition) learning Note _6_ Standard Template Library _set collection container

Related Article

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.