我可以定义一个关键是一个结构的地图吗? [英] Can I define a map whose key is a structure?
本文介绍了我可以定义一个关键是一个结构的地图吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在C ++中执行?
and how can I do it in C++?
推荐答案
您可以使用任何类型作为地图键,只要它实现了一个运算符<
(加上存储在容器中的值的通常的拷贝和分配要求)。
You can use any type as a map key, as long as it implements an operator<
(plus the usual copy-and-assign requirements for values stored in containers).
例如:
struct example { int x; }
bool operator < (const example &l, const example &r) { return l.x < r.x; }
std::map<example, int> values;
或者,您可以提供比较功能作为地图模板的第三个参数,而不是定义 operator<
。更多详情此处(参数比较
)。
Alternatively, you may provide a comparison function as the third argument of the map template instead of defining operator<
. More details here (parameter Compare
).
这篇关于我可以定义一个关键是一个结构的地图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文