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).
For instance:
struct example { int x; }
bool operator < (const example &l, const example &r) { return l.x < r.x; }
std::map<example, int> values;
Alternatively, you may provide a comparison function as the third argument of the map template instead of defining operator<. More details here (parameter Compare).
发布评论
评论(1)
您可以使用任何类型作为映射键,只要它实现
运算符<
(加上容器中存储的值的常见复制和分配要求)。例如:
或者,您可以提供一个比较函数作为地图模板的第三个参数,而不是定义
operator<
。更多详细信息此处(参数Compare
)。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).For instance:
Alternatively, you may provide a comparison function as the third argument of the map template instead of defining
operator<
. More details here (parameterCompare
).