C + + How do your own Class A valid key type for Std::map?

Source: Internet
Author: User
Tags comparable

In Java, if you want your own class to is a valid key type of the container, you just need to make it implement the Interf Ace "Comparable", and then it ' ll work properly.

How is the in C + +? I was wondering whether C + + had offered some kind of mechanism that acts like a interface in Java to enforce some "contra CT "to which a type must conform, such as Std::map, it's obvious its key type must is" comparable ".

How to ensure then the type indeed conform to the contract, through some kind of ' interface ' mechanism or what? Would the compiler does the work at compile time to check whether a type conform to the contract or not?

I looked up some C + + references in cppreference.com and cplusplus.com, I just couldn ' t find any useful information about W Hat contract std::map require on its key type.

By search on stackoverflow.com, I found this:http://stackoverflow.com/questions/19937260/ Use-user-defined-struct-as-the-key-for-map-c

So maybe I just need to define a operator< for that type?

Try it. Notice the main.cpp, if you remove the operator< on Box type. The compiler would complain when you use Box as a key type of std::map. So, the compiler does ensure that Box must conform to the contract defined by OPERATOR<.

Implement Std::map so it would offer the information to the Compiler:the Std::map::key_type must be a Typ E that have an overloaded operator< definition, which you must check for at compile time. (I ' m asking this question in stackoverflow.com, waiting for answer)

Main.cpp

1#include <iostream>2#include <map>3 4 using namespacestd;5 6 classBox {7Friend ostream&operator<< (Ostream &os,ConstBox &b);8FriendBOOL operator< (ConstBox &left,ConstBox &Right );9  Public:TenBox (intIDoubled); One~Box (); A Private: -     inti; -     DoubleD; the }; -  -Box::box (int_i,Double_d): I (_i), D (_d) {} -  +box::~Box () {} -  + BOOL operator< (ConstBox &left,ConstBox &Right ) A { at     return(Left.i <right.i); - } -  -ostream&operator<< (Ostream &os,ConstBox &b) - { -OS <<B.D; in     returnos; - } to  + intMain () - { theBox B1 (3,2), B2 (2,1), B3 (0,9); *Map<box,int>Bmap; $Bmap.insert (Pair<box,int> (B1,Ten));Panax NotoginsengBmap.insert (Pair<box,int> (B2,Ten)); -Bmap.insert (Pair<box,int> (B3,Ten)); the      for(Map<box,int>::iterator iter = Bmap.begin (); Iter! = Bmap.end (); ++iter) +     { Acout << Iter->first <<" "; the     } +cout <<Endl; -     return 0; $}

C + + How do your own Class A valid key type for Std::map?

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.