C + + Primer (Fifth edition) learning Note _9_ Standard Template Library _multimap multi-mapping container

Source: Internet
Author: User

C + + Primer (Fifth edition) learning Note _9_ Standard Template Library _multimap multi-mapping container

The multi-mapping container multimap is basically the same as the map structure, but because of the duplicate key values, the Multimap element insertion, deletion, and lookup are not the same as the map method.

1. Multimap object creation, element insertion

When inserting elements, you need to use the Insert () method and the element structure like pair<string,double> ("Jack", 300.5). As you can see, the repeating elements are sorted in the order in which they were inserted.

#include <iostream> #include <stdio.h> #include <string> #include <map> using namespace std; int main () {   multimap<string, double> str;    Insert Element   Str.insert (pair<string, double> ("Jack", +));   Str.insert (pair<string, double> ("kity",));   Str.insert (pair<string, double> ("Jack", 300.5));   Str.insert (pair<string, double> ("Memi",));   Str.insert (pair<string, double> ("Jack", 306));    For (multimap<string, double>::iterator iter = Str.begin (); Iter!=str.end (); iter++)       cout << (*iter). First << ":" << (*iter). Second << Endl;   return 0;}

Operation Result:

jack:400

jack:300.5

jack:306

kity:200

memi:500

2. Deletion of elements: erase () and clear ()

Deleting a key value takes the erase () method, and when there is a duplicate key value, it is deleted at once. Use the Clear () method to empty the elements in the Multimap container.

#include <iostream> #include <stdio.h> #include <string> #include <map> using namespace std;     int main () {multimap<string, double> str;    Insert Element Str.insert (pair<string, double> ("Jack", 400));    Str.insert (pair<string, double> ("kity", 200));    Str.insert (pair<string, double> ("Jack", 300.5));    Str.insert (pair<string, double> ("Memi", 500));     Str.insert (pair<string, double> ("Jack", 306)); For (multimap<string, double>::iterator iter = Str.begin (); Iter!=str.end (); iter++) cout << (*iter). Fi     rst<< ":" << (*iter). Second << Endl;    Remove Jack int n = str.erase ("Jack");    cout << "Number of deleted elements:" << n <<endl;    cout << "deleted elements:" << Endl; For (multimap<string, double>::iterator iter = Str.begin (); Iter!=str.end (); iter++) cout << (*iter). Fi     rst<< ":" << (*iter). Second << Endl;    Empty str.clear (); if (Str.size ()= = 0) cout << "element is empty" << Endl;    else cout << "Number of elements:" << Endl; return 0;}

Operation Result:

jack:400

jack:300.5

jack:306

kity:200

memi:500

Number of deleted elements: 3

The element after deletion:

kity:200

memi:500

Element is empty

3. Searching for elements

Because Multimap has duplicate key values, the Find () method returns only the iterator position of the first element. If the key value is not found, the end () iterator position is returned.

#include <iostream> #include <stdio.h> #include <string> #include <map> using namespace std;    int main () {multimap<string, double> str;   Insert Element Str.insert (pair<string, double> ("Jack", 400));   Str.insert (pair<string, double> ("kity", 200));   Str.insert (pair<string, double> ("Jack", 300.5));   Str.insert (pair<string, double> ("Memi", 500));    Str.insert (pair<string, double> ("Jack", 306)); For (multimap<string, double>::iterator iter = Str.begin (); Iter!=str.end (); iter++) cout << (*iter). Fir    St << ":" << (*iter). Second << Endl;   Find key values cout << "results found:";   Multimap<string, Double>::iterator iter;   iter = Str.find ("Jack");   if (iter = Str.end ()) cout << (*iter). First << ":" << (*iter). Second << Endl;    else cout << "No results found" << Endl;   cout << "results found:";   iter = Str.find ("Nacy"); if (iter = Str.end ()) cout << (*iter). First << ":" << (*iter). Second << Endl;   else cout << "No results found" << Endl; return 0;}

Operation Result:

jack:400

jack:300.5

jack:306

kity:200

memi:500

Results found for: jack:400

Results found: no results found

4. 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, writing a comparison function is consistent with the map. 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 Mutlmap:

#include <iostream> #include <stdio.h> #include <string> #include <map> using namespace std; struct mycomp{    bool Operator () (string A, string b)    {       return a > B;    }}; int main () {    multimap< String, double, mycomp> str;     Insert Element    Str.insert (pair<string, double> ("Jack", +));    Str.insert (pair<string, double> ("kity",));    Str.insert (pair<string, double> ("Jack", 300.5));    Str.insert (pair<string, double> ("Memi",));    Str.insert (pair<string, double> ("Jack", 306));     For (multimap<string, double, mycomp>::iterator iter = Str.begin (); Iter!=str.end (); iter++)        cout << (* ITER) .first<< ":" << (*iter). Second << Endl;    return 0; }

Operation Result:

memi:500

kity:200

jack:400

jack:300.5

jack:306

(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 <string> #include < Map> using namespace std;  struct info{string name;  Float score;  BOOL Operator < (Info a) const {return A.score < score;  }};     int main () {multimap<info, double> str;    Insert element Info info;    Info.name = "Jack";    Info.score = 60;     Str.insert (Pair<info, double> (Info, 400));    Info.name = "Bomi";    Info.score = 80;     Str.insert (Pair<info, double> (Info, 200));    Info.name = "Peti";    Info.score = 80;     Str.insert (Pair<info, double> (Info, 300.5));    Info.name = "Kity";    Info.score = 70;     Str.insert (Pair<info, double> (Info, 500)); For (multimap<info, double>::iterator iter = Str.begin (); Iter!=str.end (); iter++) {cout << (*iter)        .second<< ":";    cout << ((*iter). First). Name << "" << ((*iter). First). Score << Endl; } return 0; }

Operation Result:

200:bomi 80

300.5:peti 80

500:kity 70

400:jack 60

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

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