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 ;
}

最新文章

  1. Magento代码之订单创建流程
  2. 设置MYECLIPSE的自动补全
  3. GBDT(MART) 迭代决策树简介
  4. HDU 5752 Sqrt Bo (思维题) 2016杭电多校联合第三场
  5. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q115-Q117)
  6. JQuery图片延迟加载插件,动态获取图片长宽尺寸
  7. 2015南阳CCPC H - Sudoku 暴力
  8. getBoundingClientRect()兼容性处理
  9. 关于SWT/JFace中其他常用的事件
  10. /usr/bin/ld: cannot find *** 的处理
  11. webservice2-wsimport的使用
  12. app测试--稳定性测试
  13. Repaints and Reflows 重绘和重排版
  14. 编写高质量的Python代码系列(七)之协作开发
  15. UGUI血条
  16. sublime快捷键的使用
  17. iOS 10 应用内跳转到系统设置
  18. BZOJ1299[LLH邀请赛]巧克力棒——Nim游戏+搜索
  19. Docker MySQL5.5镜像
  20. Java反射机制二 获取方法的返回值或参数的泛型信息

热门文章

  1. pythonGUI菜单栏和弹出菜单
  2. KVC与KVO的不同
  3. [测试技术分享]easyFuzzer使用案例分享
  4. ActiveMQ实战-集群
  5. Bootstrap幻灯片
  6. java.sql.SQLException: Io 异常: Got minus one from a read call
  7. java 的 &amp;和&amp;&amp;的区别
  8. Java计算两个日期相差的天数
  9. http://blog.csdn.net/rosten/article/details/17068285
  10. flume分布式日志收集系统操作