C + + Primer learning Note _10_ Standard Template Library _map and set comparison
|
Set |
Multiset |
Create |
Set<int> Str |
Multiset<string> Str |
Insert |
Str.insert (8) |
Str.insert ("abc") |
Traverse |
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 |
Empty |
Str.clear () |
Str.clear () |
Current capacity |
Str.size () |
Str.size () |
Find |
iter = Str.find () Not found, return Str.end () |
iter = Str.find () Not found, return Str.end () |
unstructured 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 |
Structure comparison function ( If you want 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)) |
Traverse |
Map<string, Float>::iterator ITER |
Multiset<string, Double>::iterator ITER |
Output |
(*iter). First (*iter). Second |
(*iter). First (*iter). Second |
Delete |
n = str.erase ("Jack") The number of deletions can only be 0 or 1 |
n = str.erase ("Jack") Erase () returns the number of deleted elements |
Empty |
Str.clear () |
Str.clear () |
Current capacity |
Str.size () |
Str.size () |
Find |
iter = Str.find () Not found, return Str.end () |
iter = Str.find () Not found, return Str.end () |
unstructured 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 |
Structure comparison function ( If you want 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; } } |
Reference:
"C + + Primer Fourth Edition"
"ACM Program Design"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + Primer learning Note _10_ Standard Template Library _map and set comparison