C ++ Primer (fifth edition) Study Notes _ 9 _ Standard Template Library _ multimap multi-map container, _ 9_multimap

Source: Internet
Author: User

C ++ Primer (fifth edition) Study Notes _ 9 _ Standard Template Library _ multimap multi-map container, _ 9_multimap

C ++ Primer (fifth edition) Study Notes _ 9_standard templates_multimap multi-map container

The multimap container multimap and map structures are basically the same, but because duplicate key values exist, the multimap element insertion, deletion, and search methods are different from those of map.

 

1. multimap object creation and element insertion

When inserting an element, you must use the insert () method and element structure similar to pair <string, double> ("Jack", 300.5. We can see that duplicate elements are sorted by insertion order.

# Include <iostream> # include <stdio. h >#include <string >#include <map> using namespace std; int main () {multimap <string, double> str; // Insert the str element. 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, doub Le >:: iterator iter = str. begin (); iter! = Str. end (); iter ++) cout <(* iter ). first <":" <(* iter ). second <endl; return 0 ;}

Running result:

Jack: 400

Jack: 300.5

Jack: 306

Kity: 200

Memi: 500

 

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

The erase () method is used to delete a key value. If duplicate key values exist, they are deleted at one time. The clear () method can be used to clear 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 the str element. 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, doub Le >:: iterator iter = str. begin (); iter! = Str. end (); iter ++) cout <(* iter ). first <":" <(* iter ). second <endl; // Delete Jack int n = str. erase ("Jack"); cout <"Number of deleted elements:" <n <endl; cout <"deleted element:" <endl; for (multimap <string, double >:: iterator iter = str. begin (); iter! = Str. end (); iter ++) cout <(* iter ). first <":" <(* iter ). second <endl; // clear str. clear (); if (str. size () = 0) cout <"element is blank" <endl; else cout <"number of elements:" <endl; return 0 ;}

Running result:

Jack: 400

Jack: 300.5

Jack: 306

Kity: 200

Memi: 500

Number of deleted elements: 3

Deleted elements:

Kity: 200

Memi: 500

Element is empty

 

3. Search for elements

Because multimap has duplicate key values, the find () method returns only the iterator position of the first element. If this 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 the str element. 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, doub Le >:: iterator iter = str. begin (); iter! = Str. end (); iter ++) cout <(* iter ). first <":" <(* iter ). second <endl; // search key value cout <"Result:"; multimap <string, double >:: iterator iter; iter = str. find ("Jack"); if (iter! = Str. end () cout <(* iter ). first <":" <(* iter ). second <endl; else cout <"no result found" <endl; cout <"Result:"; iter = str. find ("Nacy"); if (iter! = Str. end () cout <(* iter ). first <":" <(* iter ). second <endl; else cout <"no result found" <endl; return 0 ;}

Running result:

Jack: 400

Jack: 300.5

Jack: 306

Kity: 200

Memi: 500

Result: Jack: 400

The result is not found.

 

4. Custom comparison functions

By default, elements are inserted in the ascending order of key values. Since the internal data structures are both red and black trees, the comparison functions are consistent with those of map. There are two writing methods,

(1) If the element is not a struct, You can compile a comparison function. The following describes how to insert elements into mutlmap in a sequence from large to small:

# 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 the str element. 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, myComp >:: iterator iter = str. begin (); iter! = Str. end (); iter ++) cout <(* iter ). first <":" <(* iter ). second <endl; return 0 ;}

Running result:

Memi: 500

Kity: 200

Jack: 400

Jack: 300.5

Jack: 306

 

(2) If the element is a struct, you can directly write the comparison function in the struct.

# Include <iostream> # include <stdio. h ># include <string >#include <map> using namespace std; struct Info {string name; float score; bool operator <(Info a) const {return. score <score ;}}; int main () {multimap <Info, double> str; // Insert the element 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 ;}

Running result:

200: Bomi 80

300.5: Peti 80

500: Kity 70

400: Jack 60

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.