C + + Primer learning Note _10_ Standard Template Library _map and set comparison

Source: Internet
Author: User

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

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.