STL Templates _MULTIMAP_ Smart pointers as key values

Source: Internet
Author: User

Type of key value for map

-Can be a custom type (object, function pointer, smart pointer 、。。。 )

-but there are side effects-a variety of problems arise when you define a type key value that cannot be sorted by the system's own < or >.

-so you need to define your own comparator for processing

Solve:

-If the key value is an object type

-You can overload the < or > operator yourself in a class

-You do not need to overload the = =

-Because the ordering of the elements is based on <, and the > is compared

eg

-a > B is b <= A;

-This is a bit of a question. I want to add ....

classkey{ Public: Key (intival): _ival (ival) {}int_ival;};BOOL operator< (Constkey& left,Constkey&Right ) {        //return Left._ival < Right._ival;     return false; //if always return False, Amap.size () is always 1. Any data coming in is/a >= b && b >= a so it's always equal.}

-When we are unable to modify the class, we can process the comparison function in the class for external use

-This is a bit of a question. I want to add ....

eg

bool Compare (constconst sale_item& RHS) {     return Lhs.book <  bool (*COMP) (constconst sale_item&

Reference:

-http://www.zhihu.com/question/34290683?sort=created

-http://blog.csdn.net/ultrahook/article/details/8146529

-http://bbs.csdn.net/topics/360183804

STL Template _MULTIMAP_ Smart pointer as key value

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.