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. 【暴力】洛谷 P2038 NOIP2014提高组 day2 T1 无线网络发射器选址

    暴力枚举. #include<cstdio> #include<algorithm> using namespace std; ][],d,n,x,y,z,num,ans=-; ...

  2. 【动态规划】【二分】【最长不下降子序列】洛谷 P1020 导弹拦截

    最长不下降子序列的nlogn算法 见 http://www.cnblogs.com/mengxm-lincf/archive/2011/07/12/2104745.html 这题是最长不上升子序列,倒 ...

  3. Spring整合jdbc-jdbc模板api详解

    1, package com.songyan.jdbc2; public class User { private int id; private String name; public int ge ...

  4. Chrome插件在页面上直接绑定JavaScript事件提示Refused to execute inline event handler because it violates the following Co

    Chrome插件问了安全是不提倡在页面上直接写JavaScript的,如果出现了这个提示,其实也没有什么,同样可以运行. 从Chrome Extenstion V2开始,不允许执行任何inline j ...

  5. zk watch机制及创建node机制

    Watch(监视) 当指定的znode或znode的子数据更改时,监视器会显示通知.你只能在 get 命令中设置watch. 语法 get /path [watch] 1 示例 get /FirstZ ...

  6. [Git] git merge和rebase的区别

    git merge 会生成一个新得合并节点,而rebase不会 比如: D---E test / A---B---C---F master 使用merge合并, 为分支合并自动识别出最佳的同源合并点: ...

  7. Cobbler安装CentOS 7网卡命名修改

    准备上线CentOS 7.x,在Cobbler上导入后,发现网卡名称变成了eno1这样的,好吧,那就添加两个内核参数上去,让它变回eth0. cobbler profile edit --name=C ...

  8. javascript快速入门21--DOM总结

    跨浏览器开发 市场上的浏览器种类多的不计其数,它们的解释引擎各不相同,期待所有浏览器都一致的支持JavaScript,CSS,DOM,那要等到不知什么时候,然而开发者不能干等着那天.历史上已经有不少方 ...

  9. 一起來玩鳥 Starling Framework(5)Multi-Touch

    這篇來談談Starling的Multi-Touch.前一篇也提到,Multi-Touch一樣是監聽TouchEvent.TOUCH,然後由TouchEvent的e.getTouches()取回多點的資 ...

  10. new AppiumDriver<>(new URL(url), capabilities) 报错 java.lang.NoSuchMethodError: com.google.common.base.Throwables.throwIfUnchecked(Ljava/lang/Throwable;)V

    2017-10-11 17:37:02.102 INFO c.u.a.r.PrepareDriver:41 - appium server url : http://127.0.0.1:4723/wd ...