[英]C++ STL map with custom comparator storing null pointers
I'm trying to write a copy constructor for an object managing a STL map containing pointers, where the key is a string. 我正在尝试为管理包含指针的STL映射的对象编写复制构造函数,其中键是一个字符串。 However, when I attempt to insert new values in the map, the pointers are set to NULL: 但是,当我尝试在地图中插入新值时,指针将设置为NULL:
// ...
for(std::map<std::string, data_base*, order>::const_iterator it = other.elements.begin();
it != other.elements.end(); ++it){
data_base *t = it->second->clone();
std::cout << "CLONE: " << std::hex << t << std::endl;
elements[it->first] = t;
std::cout << "INSERTED: " << std::hex << elements[it->first] << std::endl;
}
// ...
other
is the object being copied and elements
the map. other
是被复制的对象和elements
的映射。 The clone()
method returns a pointer to a new object (via new
). clone()
方法返回一个指向新对象的指针(通过new
)。
Running the code above I get something like: 运行上面的代码我得到类似的东西:
CLONE: 0xcfbbc0
INSERTED: 0
I'm not a very experienced programmer and this issue is probably simple to fix, but I didnt find any solution to it searching around. 我不是一个非常有经验的程序员,这个问题可能很容易修复,但我没有找到任何解决方案来搜索它。
Thanks a lot for your time. 非常感谢你的时间。
I don't see any problem with this code, other than maybe 我没有看到这个代码有任何问题,除了可能
std::map<std::string, data_base*, order>::const_iterator it
Here order
gives the key comparator to use to sort the pairs contained in the map (often implemented as a tree). 这里的order
给出了用于对映射中包含的对进行排序的关键比较器(通常实现为树)。
Maybe you're doing something wrong in it, making your [] operator don't find the right ke, making your last line logging a new pair with a null ptr. 也许你在做错了,让你的[]操作符找不到合适的ke,让你的最后一行记录一个带有空ptr的新对。
First, try without that order
, using the default key-comparator (std::less), then if it don't work, post your order
definition and the map declaration. 首先,尝试没有该order
,使用默认的key-comparator(std :: less),然后如果它不起作用,发布你的order
定义和地图声明。 If it's not enough, just provide a simple complete program that reproduce the problem. 如果还不够,只需提供一个简单的完整程序来重现问题。
I just wrote a simple similar test, using the default key-comparator : 我刚刚使用默认的key-comparator编写了一个简单的类似测试:
#include <map>
#include <string>
#include <iostream>
struct Data
{
int k;
Data* clone() { return new Data(); }
};
typedef std::map< std::string, Data* > DataMap;
DataMap data_map;
int main()
{
data_map[ "hello" ] = new Data();
data_map[ "world" ] = new Data();
DataMap other_map;
for( DataMap::const_iterator it = data_map.begin(); it != data_map.end(); ++it)
{
Data*t = it->second->clone();
std::cout << "CLONE: " << std::hex << t << std::endl;
other_map[it->first] = t;
std::cout << "INSERTED: " << std::hex << other_map[it->first] << std::endl;
}
std::cin.ignore();
return 0;
}
On VS2010SP1, this outputs : 在VS2010SP1上,输出:
CLONE: 00034DD0
INSERTED: 00034DD0
CLONE: 00035098
INSERTED: 00035098
So it should be the problem, or maybe you're doing something wrong before. 所以应该是问题,或者你之前做错了。
Try this out, to help debug the issue. 试试这个,以帮助调试问题。 I'd recommend double-checking that the order
function is correct. 我建议仔细检查order
功能是否正确。 You can remove it to use std::less<T>
, which is known to work. 您可以将其删除以使用已知可用的std::less<T>
。
// ...
typedef std::map<std::string, data_base*, order> string_db_map;
for(string_db_map::const_iterator it = other.elements.begin();
it != other.elements.end();
++it)
{
data_base *t = it->second->clone();
std::cout << "CLONE: " << std::hex << t << std::endl;
std::pair<string_db_map::iterator, bool) result = elements.insert(
string_db_map::value_type( it->first, t));
if ( !result.second )
{
std::cout << "element['" << it->first << "'] was already present, and replaced." << std::endl;
}
std::coud << "INSERTED [iterator]: " << std::hex << (*result.first).second << std::endl;
std::cout << "INSERTED [indexed]: " << std::hex << elements[it->first] << std::endl;
}
// ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.