c++ how to make your own class a valid key type for std::map?
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?的更多相关文章
- Mapreduce的文件和hbase共同输入
Mapreduce的文件和hbase共同输入 package duogemap; import java.io.IOException; import org.apache.hadoop.co ...
- mapreduce多文件输出的两方法
mapreduce多文件输出的两方法 package duogemap; import java.io.IOException; import org.apache.hadoop.conf ...
- mapreduce中一个map多个输入路径
package duogemap; import java.io.IOException; import java.util.ArrayList; import java.util.List; imp ...
- In-Memory:内存数据库
在逝去的2016后半年,由于项目需要支持数据的快速更新和多用户的高并发负载,我试水SQL Server 2016的In-Memory OLTP,创建内存数据库实现项目的负载需求,现在项目接近尾声,系统 ...
- 01.SQLServer性能优化之---水平分库扩展
汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 第一次引入文件组的概念:http://www.cnblogs.com/dunitian/ ...
- Hadoop 中利用 mapreduce 读写 mysql 数据
Hadoop 中利用 mapreduce 读写 mysql 数据 有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv.uv 数据,然后为了实时查询的需求,或者一些 OLAP ...
- 关于DOM的操作以及性能优化问题-重绘重排
写在前面: 大家都知道DOM的操作很昂贵. 然后贵在什么地方呢? 一.访问DOM元素 二.修改DOM引起的重绘重排 一.访问DOM 像书上的比喻:把DOM和JavaScript(这里指ECMScri ...
- Angular2入门系列教程7-HTTP(一)-使用Angular2自带的http进行网络请求
上一篇:Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数 感觉这篇不是很好写,因为涉及到网络请求,如果采用真实的网络请求,这个例子大家拿到手估计还要自己写一个web ...
- 防御XSS攻击-encode用户输入内容的重要性
一.开场先科普下XSS 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶 ...
- Fis3前端工程化之项目实战
Fis3项目 项目目录结构: E:. │ .gitignore │ fis-conf.js │ index.html │ package.json │ README.md │ ├─material │ ...
随机推荐
- 【二分答案】【最短路】bzoj1614 [Usaco2007 Jan]Telephone Lines架设电话线
对于二分出的答案x而言,验证答案等价于将所有边权>x的边赋成1,否则赋成0,然后判断从1到n的最短路是否<=K. #include<cstdio> #include<cs ...
- python3-开发面试题(python)6.23基础篇(2)
1.请至少列举5个 PEP8 规范(越多越好). 一.代码编排 1.缩进.4个空格的缩进,不使用Tap,更不能混合使用Tap和空格 2.每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换行点要在 ...
- JNI之常用函数大全
要素 :1. 该函数大全是基于C语言方式的,对于C++方式可以直接转换 ,例如,对于生成一个jstring类型的方法转换分别如下: C编程环境中使用方法为:(*env) ->NewString ...
- 打补丁以及WebLogic Server的版本
12.1.2开始采用了Oracle传统的opatch打补丁的方式,但在此之前,包括 10.3.x版本以及12.1.1版本还是通过bea的smart update方式来进行. smart update基 ...
- linux 关闭端口
第一步查看那些程序在使用该端口 netstat -anp或者netstat -tulnp 第二步杀死进程 kill -9 xxx,杀死某个进程 killall 进程名
- RenderMonkey基本使用方法【转】
RenderMonkey基本使用方法 楔子: 差不多从年中开始由于工作需要,开始研究Direct3D,这是继大二开始自学DX开始,睽违了6年后再重新学习DX.虽然时间很久了,但是幸亏还是有点基础,所以 ...
- FX Composer VS RenderMonkey 【转】
http://blog.csdn.net/debugconsole/article/details/50905398 FX COMPOSER 其实编辑一个shader到debug它,有很多方法,很多方 ...
- Leagal or Not —— 拓扑排序(王道)
Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is so h ...
- [Parcel] Running TypeScript with parcel-bundler
TO get started with TypeScirpt quickly in your local computer is using parcel-bunlder: npm i -g parc ...
- 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-报错0X4650,18000错误怎么办
驱动器未就绪错误,请重启驱动器 注意,即便是报这个错,仍然有可能上电成功,这样的话可以直接在TwinCAT中清除错误即可 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http: ...