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

How about in C++? I was wondering whether C++ has offered some kind of mechanism that acts like an interface in Java to enforce some "contract" to which a type must conform, such as std::map, it's obvious its key type must be "comparable".

How to ensure that the type indeed conform to the contract, through some kind of "interface" mechanism or what? Will the compiler do 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 what 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 an operator< for that type ?

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

But how to implement std::map so that it would offer the information to the compiler: the std::map::key_type must be a type that has an overloaded operator< definition, you must check this at compile time. (I'm asking this question on stackoverflow.com, waiting for answer, http://stackoverflow.com/questions/27078796/how-is-stdmap-implemented-so-it-can-require-its-key-type-to-be-comparable)

main.cpp

 #include <iostream>
#include <map> using namespace std; class Box {
friend ostream& operator<<(ostream &os, const Box &b);
friend bool operator<(const Box &left, const Box &right);
public:
Box(int i, double d);
~Box();
private:
int i;
double d;
}; Box::Box(int _i, double _d):i(_i), d(_d) {} Box::~Box() {} bool operator<(const Box &left, const Box &right)
{
return (left.i < right.i);
} ostream& operator<<(ostream &os, const Box &b)
{
os << b.d;
return os;
} int main()
{
Box b1(,), b2(,), b3(, );
map<Box, int> bmap;
bmap.insert(pair<Box,int>(b1, ));
bmap.insert(pair<Box,int>(b2, ));
bmap.insert(pair<Box,int>(b3, ));
for (map<Box,int>::iterator iter = bmap.begin(); iter != bmap.end(); ++iter)
{
cout << iter->first << " ";
}
cout << endl;
return ;
}

c++ how to make your own class a valid key type for std::map?的更多相关文章

  1. Mapreduce的文件和hbase共同输入

    Mapreduce的文件和hbase共同输入 package duogemap;   import java.io.IOException;   import org.apache.hadoop.co ...

  2. mapreduce多文件输出的两方法

    mapreduce多文件输出的两方法   package duogemap;   import java.io.IOException;   import org.apache.hadoop.conf ...

  3. mapreduce中一个map多个输入路径

    package duogemap; import java.io.IOException; import java.util.ArrayList; import java.util.List; imp ...

  4. In-Memory:内存数据库

    在逝去的2016后半年,由于项目需要支持数据的快速更新和多用户的高并发负载,我试水SQL Server 2016的In-Memory OLTP,创建内存数据库实现项目的负载需求,现在项目接近尾声,系统 ...

  5. 01.SQLServer性能优化之---水平分库扩展

    汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 第一次引入文件组的概念:http://www.cnblogs.com/dunitian/ ...

  6. Hadoop 中利用 mapreduce 读写 mysql 数据

    Hadoop 中利用 mapreduce 读写 mysql 数据   有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv.uv 数据,然后为了实时查询的需求,或者一些 OLAP ...

  7. 关于DOM的操作以及性能优化问题-重绘重排

     写在前面: 大家都知道DOM的操作很昂贵. 然后贵在什么地方呢? 一.访问DOM元素 二.修改DOM引起的重绘重排 一.访问DOM 像书上的比喻:把DOM和JavaScript(这里指ECMScri ...

  8. Angular2入门系列教程7-HTTP(一)-使用Angular2自带的http进行网络请求

    上一篇:Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数 感觉这篇不是很好写,因为涉及到网络请求,如果采用真实的网络请求,这个例子大家拿到手估计还要自己写一个web ...

  9. 防御XSS攻击-encode用户输入内容的重要性

    一.开场先科普下XSS 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶 ...

  10. Fis3前端工程化之项目实战

    Fis3项目 项目目录结构: E:. │ .gitignore │ fis-conf.js │ index.html │ package.json │ README.md │ ├─material │ ...

随机推荐

  1. 【数论】【枚举约数】【欧拉函数】bzoj2705 [SDOI2012]Longge的问题

    ∵∑gcd(i, N)(1<=i <=N) =k1*s(f1)+k2*s(k2)+...+km*s(km) {ki是N的约数,s(ki)是满足gcd(x,N)=ki(1<=x< ...

  2. jvm-监控指令-jinfo

    格式: jinfo [option] pid 作用: 实时查看和调整虚拟机各项参数. 使用步骤: 1.查看:   jinfo vmid. 2.查看指定的参数:  jinfo -flag  参数名  v ...

  3. delphi执行cmd命令和bat文件

    转载地址:http://blog.csdn.net/hutao1101175783/article/details/42807063 cmd:='echo d | Xcopy '+BasePath+' ...

  4. linux如何启动/停止/重启MySQL

    如何启动/停止/重启MySQL 一.启动方式 1.使用 service 启动:service mysqld start2.使用 mysqld 脚本启动:/etc/inint.d/mysqld star ...

  5. 【js】判断浏览器是否IE浏览器

    搜罗各种方法来判断浏览器是否为IE浏览器 1.最简单的[来自:http://www.cnblogs.com/heganlin/p/5889743.html] if(!+[1,]){ layer.msg ...

  6. Sublime text JsFormat插件的安装

    javascript格式化插件JsFormat 1.下载这插件包 https://github.com/jdc0589/JsFormat 2.点击菜单:Preferences->Browse P ...

  7. HashMap深度解析(一)

    HashMap可以说是Java中最常用的集合类框架之一,是Java语言中非常典型的数据结构,我们总会在不经意间用到它,很大程度上方便了我们日常 开发.在很多Java的笔试题中也会被问到,最常见的,“H ...

  8. vue中的组件,Component元素,自定义路由,异步数据获取

    组件是Vue最强大的功能之一.组件是一组可被复用的具有一定功能,独立的完整的代码片段,这个代码片段可以渲染一个完整视图结构组件开发如何注册组件?第一步,在页面HTML标签中使用这个组件名称,像使用DO ...

  9. centos和ubuntu下pycharm无法输入中文的解决办法

    编辑启动的脚本文件 vim /usr/bin/pycharm ubuntu下添加 export GTK_IM_MODULE=fcitx export QT_IM_MODULE=fcitx export ...

  10. javascript数据类型检测方法

    一.字符串.数字.布尔值.undefined的最佳选择市使用 typeof 运算符进行检测: 对于字符串,typeof 返回"string" 对于数字,typeof 返回" ...