简体   繁体   English

将指针映射转换为对象映射

[英]Converting a map of pointers to map of of objects

I have a map which has pointers to objects as keys (values are int). 我有一个映射,该映射具有指向对象的指针作为键(值是int)。 Given this, I want to create a map that is identical to the original map, but I want the keys to be the corresponding objects (aka dereferenced keys) 鉴于此,我想创建一个与原始地图相同的地图,但是我希望这些键成为相应的对象(又名解除引用的键)

Here is my code: 这是我的代码:

map<const Point*, int> m1 = createMap();
map<Point,int> temp;
for_each(q.begin(), q.end(), [&temp](pair<const Point*, int> p){temp.insert(make_pair(*(p.first),p.second));});

What I'm trying to do is call a lambda that simpley derefrences the keys of the initial map. 我想做的是调用一个lambda,它可以简单地取消初始地图的键。

The compiler however complains that "/usr/include/c++/4.8/bits/stl_function.h|235|error: no match for 'operator<' (operand types are 'const Point' and 'const Point')|" 但是,编译器抱怨“ / usr / include / c ++ / 4.8 / bits / stl_function.h | 235 |错误:'operator <'不匹配(操作数类型为'const Point'和'const Point')|“

Am I making a foolish mistake somewhere? 我在某个地方犯了一个愚蠢的错误吗?

You have to define operator< for your class Point. 您必须为类Point定义operator <

A map is an ordered collection of elements. 映射是元素的有序集合。 Hence you need to implement a function to order them. 因此,您需要实现一个函数来订购它们。 By default, that function is operator< , but you can specify a different one in the constructor if you want. 默认情况下,该函数为operator < ,但是您可以根据需要在构造函数中指定其他函数。

Se here, for example: 例如,这里:

http://www.cplusplus.com/reference/map/map/ http://www.cplusplus.com/reference/map/map/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM