C ++ Primer study note _ 10 _ comparison of the standard template library _ map and set, _ 10_map
C ++ Primer study note _ 10 _ comparison of the standard template library _ map and set
|
Set |
Multiset |
Create |
Set <int> str |
Multiset <string> str |
Insert |
Str. insert (8) |
Str. insert ("abc ") |
Traversal |
Set <int>: iterator iter |
Multiset <string >:: iterator iter |
Output |
* Iter |
* Iter |
Delete |
N = str. erase (8) The number of deletions can only be 0 or 1 |
N = str. erase ("123 ") Erase () returns the number of deleted elements. |
Clear |
Str. clear () |
Str. clear () |
Current capacity |
Str. size () |
Str. size () |
Search |
Iter = str. find () No. str. end () is returned () |
Iter = str. find () No. str. end () is returned () |
Non-struct comparison Function |
Struct myComp { Bool operator () (int a, int B) { Return a> B; } } Definition: set <int, myComp> str |
Struct myComp { Bool operator () (string a, string B) { Return a> B; } }; Multiset <string, myComp> str |
Struct comparison Function (To sort from small to large, use the ">" sign) |
Struct Info { String name; Float score; Bool operator <(Info a) const { Return a. score <score; } } |
Struct Info { String name; Float score; Bool operator <(Info a) const { Return a. score <score; } }; |
|
Map |
Multimap |
Create |
Map <string, float> str |
Multimap <string, double> str |
Insert |
Str ["Jack"] = 98.5 |
Str. insert (pair <string, double> ("Jack", 400 )) |
Traversal |
Map <string, float >:: iterator iter |
Multiset <string, double >:: iterator iter |
Output |
(* Iter). first (* Iter). second |
(* Iter). first (* Iter). second |
Delete |
N = str. erase ("Jack ") Number of deletions, which can only be 0 or 1 |
N = str. erase ("Jack ") Erase () returns the number of deleted elements. |
Clear |
Str. clear () |
Str. clear () |
Current capacity |
Str. size () |
Str. size () |
Search |
Iter = str. find () No. str. end () is returned () |
Iter = str. find () No. str. end () is returned () |
Non-struct comparison Function |
Struct myComp { Bool operator () (int a, int B) { Return a> B; } } Definition: set <int, char, myComp> str |
Struct myComp { Bool operator () (string a, string B) { Return a> B; } } Definition: set <string, double, myComp> str |
Struct comparison Function (To sort from small to large, use the ">" sign) |
Struct Info { String name; Float score; Bool operator <(Info a) const { Return a. score <score; } } |
Struct Info { String name; Float score; Bool operator <(Info a) const { Return a. score <score; } } |
Refer:
C ++ primer version 4
ACM Program Design
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.