C++ 如何在地图中存储指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4088387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:31:26  来源:igfitidea点击:

how to store pointers in map

c++stlmap

提问by Venkata

I am working on one of the project which requires

我正在从事需要的项目之一

class MyObj;

map<string, MyObj*> myMap;

Here logic is here to map file name to MyObj class.

这里的逻辑是将文件名映射到 MyObj 类。

If I try to insert following

如果我尝试插入以下内容

string strFilename = "MyFile";
MyObj* pObj  = new MyObj();

myMap.insert(strFileName, pObj); // This line throwing following error.

no matching function for call to 'std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*> > >::insert(std::string&, void*)'

没有匹配的函数调用 'std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*> > >::insert(std::string&, void*)'

Can any one please help me how to solve this. Are is there better way we can do this using STL

任何人都可以帮助我如何解决这个问题。有没有更好的方法可以使用 STL 做到这一点

回答by Moo-Juice

I've typedef'd this stuff to make it more readable...

我已经对这些东西进行了 typedef 以使其更具可读性...

typedef std::map<std::string, MyObj*> MyMap;
typedef std::pair<std::string, MyObj*> MyPair;

MyMap myMap;
string strFilename = "MyFile";
MyObj* pObj = new MyObj();
myMap.insert(MyPair(strFilename, pObj));

回答by robev

std::map requires a pair when you use the insert function.

使用插入函数时, std::map 需要一对。

You have two options, either:

您有两种选择:

myMap[strFileName] = pObj;

Or:

或者:

myMap.insert(std::make_pair(strFileName,pObj));

回答by Prasoon Saurav

myMap.insert(strFileName, pObj);

myMap.insert(strFileName, pObj);

Use make_pair()inside insert()

make_pair()内部使用insert()

#include <utility>
//...
myMap.insert (std::make_pair(strFileName, pObj) ) ;

回答by Steve M

There is no insertmember that takes a key and a value as arguments. Just use the index operator:

没有insert将键和值作为参数的成员。只需使用索引运算符:

myMap[fileName] = obj;

回答by Strahd_za

map<string,MyObj*> myMap;

string strFilename = "MyFile";  
MyObj* pObj = new MyObj();

myMap[strFilename] = pObj;

Should work just fine. Just remember clearing the map wont free up the memory the pointers are pointing to.

应该工作得很好。请记住清除地图不会释放指针指向的内存。

回答by Paul Rubel

Insert wants a pair, try something like this:

插入想要一对,试试这样的:

mymap.insert ( pair<string,MyObj*>(strFilename,pObj) );

回答by Denis Shevchenko

Try boost::ptr_map (from boost::ptr_container library). And with boost::assign library you even do not need to call new manually:

尝试 boost::ptr_map (来自 boost::ptr_container 库)。使用 boost::assign 库,您甚至不需要手动调用 new :

class MyObj {
    MyObj() { /* some... */ }
};

typedef boost::ptr_map< string, MyObj > MyMap;
MyMap m;

using namespace boost::assign;

ptr_map_insert( m )( "first" )( "second" )( "third" );

In this case three elements will added in m (with keys "first", "second" and "third" and with three created objects MyObj()).

在这种情况下,三个元素将添加到 m(带有键“first”、“second”和“third”以及三个创建的对象 MyObj())。

Or:

或者:

class MyObj {
    MyObj( int _i ) : i( _i ) { /* some... */ }
    int i;
};

typedef boost::ptr_map< string, MyObj > MyMap;
MyMap m;

using namespace boost::assign;

ptr_map_insert( m )( "first", 1 )( "second", 2 )( "third", 3 );

In this case three elements will added in m. First element have key "first" and value MyObj( 1 ), second - key "second" and MyObj( 2 ), third - key "third" and MyObj( 3 ).

在这种情况下,三个元素将添加到 m 中。第一个元素具有键“first”和值 MyObj(1),第二个键是“second”和 MyObj(2),第三个键是“third”和 MyObj(3)。

回答by Denis Shevchenko

Try boost::ptr_map ()

试试 boost::ptr_map()

回答by Dima

You are calling insert()incorrectly. It does not take a key and a value separately, it takes a pair like this:

你调用insert()错误。它不需要单独的键和值,它需要一对像这样:


myMap.insert(pair < string, MyObj* > (strFileName, pObj));