C + + Primer (Fifth edition) learning Note _7_ Standard Template Library _multiset multi-collection container

Source: Internet
Author: User

C + + Primer (Fifth edition) learning Note _7_ Standard Template Library _multiset multi-collection container

The Multiset container multiset, like set, also uses a red-black tree to organize the element data, and the only thing not to do is that Multiset allows duplicate element key values to be inserted. The structure is as follows:

1. multiset element Insertion

#include <iostream> #include <stdio.h> #include <vector> #include <set> #include <string> using namespace Std; int main () {   multiset<string> str;    Str.insert ("abc");   Str.insert ("123");   Str.insert ("111");   Str.insert ("AAA");   Str.insert ("123");    The middle sequence iterates through all the elements in the collection for   (multiset<string>::iterator iter = Str.begin (); Iter!=str.end (); iter++)       cout < < *iter << "";   cout << Endl;    return 0;}

Operation Result:

111 123 123 AAA ABC

2. multiset element deletion

(1), using the Erase () method, you can delete all duplicate elements of a value in the Multiset object and return the number of deleted elements.

(2), use the Clear () method to clear the element.

#include <iostream> #include <stdio.h> #include <vector> #include <set> #include <string> using namespace Std;    int main () {multiset<string> str;   Str.insert ("abc");   Str.insert ("123");   Str.insert ("111");   Str.insert ("AAA");    Str.insert ("123"); The middle sequence iterates through all the elements in the collection for (Multiset<string>::iterator iter = Str.begin (); Iter!=str.end (); iter++) cout << *i   ter << "";   cout << Endl;    cout << Endl;   int n = str.erase ("123");   cout << "Number of deleted elements:" << n << Endl;   cout << "Output deleted elements" << Endl;   for (Multiset<string>::iterator iter = Str.begin (); Iter!=str.end (); iter++) cout << *iter << "";   cout << Endl;    cout << Endl;   Str.clear ();   cout << "Output deleted elements" << Endl;   for (Multiset<string>::iterator iter = Str.begin (); Iter!=str.end (); iter++) cout << *iter << "";   cout << Endl; return 0;}

Operation Result:

111 123 123 AAA ABC

Number of deleted elements: 2

The remaining elements after the output is deleted

111 AAA ABC

The remaining elements after the output is deleted

3. Find elements found ()

Use the Find () method to find the element:

Returns the iterator position of the element if it is found (returns the iterator position of the first element repeating the element if the element is duplicated), or, if found, returns the iterator position of the element (or, if there is a repetition of the element, the iterator position of the first element repeating element);

If not found, returns the end () iterator position.

#include <iostream> #include <stdio.h> #include <vector> #include <set> #include <string> using namespace Std; int main () {   multiset<string> str;    Str.insert ("abc");   Str.insert ("123");   Str.insert ("111");   Str.insert ("AAA");   Str.insert ("123");    The middle sequence iterates through all the elements in the collection for    (Multiset<string>::iterator iter =str.begin (); iter!= str.end (); iter++)       cout < < *iter << "";   cout << Endl;   cout << Endl;    Multiset<string>::iterator iter;   iter = Str.find ("123");   if (iter! = Str.end ())//Find       cout << *iter << Endl;   else  //did not find       cout << "not found" << Endl;   iter = Str.find ("BBB");   if (iter! = Str.end ())       cout << *iter << Endl;   else       cout << "not found" << Endl;   return 0;}

Operation Result:

111 123 123 AAA ABC

123

Not found


4. Custom comparison function

By default, elements are inserted in the order of the key values from small to large. Because internal data structures are red-black trees, writing comparison functions is consistent with set. 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 from the large to the small in order to insert the elements into Mutlset:

#include <iostream> #include <stdio.h> #include <vector> #include <set> #include <string> using namespace Std; struct mycomp{    bool Operator () (string A, string b)    {       return a > B;    }};  int main () {    multiset<string, mycomp> str;     Str.insert ("abc");    Str.insert ("123");    Str.insert ("111");    Str.insert ("AAA");    Str.insert ("123");     The middle sequence iterates through all the elements in the collection for    (multiset<string, Mycomp>::iterator iter = Str.begin (); Iter!=str.end (); iter++)        cout << *iter << "";    cout << Endl;    return 0;}


Operation Result:

ABC AAA 123 123 111

(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> #include <string> using namespace Std; struct info{  string name;  float score;  BOOL Operator < (Info a) const  {    return A.score < score;  }};  int main () {    multiset<info> str;     Insert element    info info;    Info.name = "Jack";    Info.score =;    Str.insert (info);     Info.name = "Bomi";    Info.score = n;    Str.insert (info);     Info.name = "Peti";    Info.score = n;    Str.insert (info);     Info.name = "Kity";    Info.score = +;    Str.insert (info);     for (Multiset<info>::iterator iter = Str.begin (); Iter!=str.end (); iter++)        cout << (*iter) .name< < "" << (*iter) score << Endl;     return 0; }

Operation Result:

Bomi 80

Peti 80

Kity 70

Jack 60

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

C + + Primer (Fifth edition) learning Note _7_ Standard Template Library _multiset multi-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.