std :: map问题 [英] std::map question

查看:61
本文介绍了std :: map问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的地图:


std :: map< string,CObject> ObjectList;


我有这样的函数:


CObject * NewObject(char * Name,CArg * Arg)

{

std :: string key = Name;

ObjectList [key] = CObject(Name,Arg);

return& ; ObjectList [Name];

}


我试图编译它抱怨没有默认构造函数

for CObject


所以我将构造函数声明更改为:

CObject :: CObject(char * Name =" Default",CArg * Arg = NULL);


只是因为我没有有效的CArg指针就无法创建它。


当我调用NewObject时它会编译崩溃,我看到它的调试器

似乎首先用正确的参数调用构造函数,然后

再次调用它而没有参数(在这种情况下使我的程序崩溃) ,

但我不明白为什么。


如果有人能解释我不知道的是什么站在这里....


另一个快速的问题,如果名称确实& ObjectList [Name]返回NULL

与地图中的键不匹配?


感谢advanec。

Well I have a map like this :

std::map <string, CObject> ObjectList;

I have a function like this :

CObject* NewObject( char* Name, CArg* Arg)
{
std::string key = Name;
ObjectList[ key] = CObject( Name, Arg);
return &ObjectList[Name];
}

I tried to compile and it complained that there was no default constructor
for CObject

So I changed my constructor declaration to :
CObject::CObject( char* Name = "Default", CArg* Arg = NULL);

Just to sse because I can''t create it without a valid CArg pointer.

it compiles when I call NewObject it crashes, with the debugger I saw it
seemed to call the constructor with the correct arguments first, and then
call it again with no arguments (which in this case makes my program crash),
but I don''t understant why.

If someon ecould explain what I don''t understand here....

An other quick question, does &ObjectList[ Name] return NULL if Name does
not match a key in the map ?

Thanks in advanec.

推荐答案

" Flzw" < FL **** @ wanadoo.fr>写道...
"Flzw" <fl****@wanadoo.fr> wrote...
我有这样的地图:

std :: map< string,CObject> ObjectList;

我有这样的函数:

CObject * NewObject(char * Name,CArg * Arg)
{
std :: string key = Name;
ObjectList [key] = CObject(Name,Arg);
return& ObjectList [Name];
}
我试图编译和它抱怨没有默认构造函数
CObject


没有要求包含的对象有默认值

c-tor _unless_你自己用它。它们需要_assignable_

和_copy-constructible_。如果您使用


ObjectList.insert(make_pair(key,CObject(Name,Arg)));


而不是


ObjectList [key] = CObject(Name,Arg);


你不需要它默认构造。

所以我将构造函数声明更改为:
CObject :: CObject(char * Name =" Default",CArg * Arg = NULL);


嗯,这是一个非常糟糕的声明。它似乎暗示你在你的CObject中使用动态内存,这意味着你可能搞砸了动态内存管理的
。比如,它被删除两次(通常会发生

,当你没有正确实现复制程序时)。阅读

三个规则并考虑动态记忆的_ownership_

分配给你的对象。


该声明的不良之处在于你传递了这些字符

和CArgs通过指向非const对象的指针,这可能意味着

而不是创建CObject自己的副本,你只需复制
那里有
指针值。好吧,希望不是。只是确保...

只是因为我没有有效的CArg指针就无法创建它。


再一次,你真的不需要这里的默认c-tor。

当我调用NewObject它崩溃时编译,用我看过的调试器它似乎首先用正确的参数调用构造函数,然后再次调用它而没有参数(在这种情况下使我的程序
崩溃),但我并不理解为什么。


因为表达式


ObjectList [key] = CObject(blah);


需要(a)创建一个空的CObject,(b)创建一个带有''blah''

参数的CObject,然后(c)将后者的赋值调用给前者。

如果有人在这里解释我不明白的东西....


你不明白几件事。获得一本好书并阅读

标准容器,它们的行为以及它们在所包含的类上放置的要求

。还得到一本好书并阅读有关动态

内存管理的内容。

另一个快速问题,如果Name没有,那么& ObjectList [Name]会返回NULL
匹配地图中的一个键?
Well I have a map like this :

std::map <string, CObject> ObjectList;

I have a function like this :

CObject* NewObject( char* Name, CArg* Arg)
{
std::string key = Name;
ObjectList[ key] = CObject( Name, Arg);
return &ObjectList[Name];
}

I tried to compile and it complained that there was no default constructor
for CObject
There is no requirement for the contained objects to have the default
c-tor _unless_ you''re using it yourself. They need to be _assignable_
and _copy-constructible_. If you used

ObjectList.insert(make_pair(key, CObject(Name,Arg)));

instead of

ObjectList[key] = CObject(Name, Arg);

you wouldn''t need it default-constructible.
So I changed my constructor declaration to :
CObject::CObject( char* Name = "Default", CArg* Arg = NULL);
Well, that''s a very bad declaration. It seems to suggest taht you are
using dynamic memory in your CObject, which means you probably screw up
the dynamic memory management. Like, it''s deleted twice (usually happens
when you don''t have the copy-c-tor correctly implemented). Read about
"the Rule of Three" and think about the _ownership_ of the dynamic memory
allocated in your objects.

The badness of that declaration is in the fact that you pass those chars
and CArgs by a pointer to non-const object, which probably means that
instead of creating the CObject''s own copies of those, you just copy the
pointer values there. Well, hopefully not. Just making sure...
Just to sse because I can''t create it without a valid CArg pointer.
Again, you really don''t need the default c-tor here.
it compiles when I call NewObject it crashes, with the debugger I saw it
seemed to call the constructor with the correct arguments first, and then
call it again with no arguments (which in this case makes my program crash), but I don''t understant why.
Because the expression

ObjectList[key] = CObject(blah);

needs to (a) create an empty CObject, (b) create a CObject with ''blah''
parameters and then (c) call the assignment from the latter to the former.
If someon ecould explain what I don''t understand here....
You don''t understand several things. Get a good book and read about
standard containers, their behaviour, and the requirements they place
on the contained classes. Also get a good book and read about dynamic
memory management.
An other quick question, does &ObjectList[ Name] return NULL if Name does
not match a key in the map ?




否。它插入一个新对象并返回对它的引用。 RTFM。


Victor



No. It inserts a new object and returns a reference to it. RTFM.

Victor


" Flzw" < FL **** @ wanadoo.fr>在消息中写道

新闻:cf ********** @ news-reader5.wanadoo.fr ...
"Flzw" <fl****@wanadoo.fr> wrote in message
news:cf**********@news-reader5.wanadoo.fr...
我有这样的地图:

std :: map< string,CObject> ObjectList;

我有这样的函数:

CObject * NewObject(char * Name,CArg * Arg)
{
std :: string key = Name;
ObjectList [key] = CObject(Name,Arg);
return& ObjectList [Name];
}
我试图编译和它抱怨说CObject没有默认的构造函数

所以我将构造函数声明更改为:
CObject :: CObject(char * Name =" Default",CArg * Arg = NULL);

只是因为我没有有效的CArg指针就无法创建它。

当我调用NewObject时它会编译崩溃,使用调试器我看到它似乎首先用正确的参数调用构造函数,然后再次调用它而没有参数(在这种情况下使我的程序
崩溃),但我不是我明白为什么。

如果有人能解释我在这里不明白的东西....


我不明白你描述的行为d,但我会解释

的行为应该是什么。


ObjectList [key] = CObject(Name,Arg);


上面的行创建了一个默认构造的CObject,并试图将

插入到std :: map中作为映射到键的值。如果已经有一个映射到键的

值,那么std :: map保持不变。然后

std :: map :: operator []返回对映射的CObject实例的引用

到key(可能是我刚才提到的默认构造对象或者一个

已经存在)。然后,调用CObject的赋值运算符,

有效地替换了存储在

std :: map中的实例的内容。


如果您不想指定默认构造函数,可以使用

std :: map :: insert而不是operator []。该函数返回一个迭代器

和一个布尔值,因此你可以检查插入是否没有在地图中插入新的

键值对然后使用在这种情况下修改现有的

值的迭代器。

另一个快速的问题,如果Name确实
不匹配,那么& ObjectList [Name]会返回NULL键入地图?
Well I have a map like this :

std::map <string, CObject> ObjectList;

I have a function like this :

CObject* NewObject( char* Name, CArg* Arg)
{
std::string key = Name;
ObjectList[ key] = CObject( Name, Arg);
return &ObjectList[Name];
}

I tried to compile and it complained that there was no default constructor
for CObject

So I changed my constructor declaration to :
CObject::CObject( char* Name = "Default", CArg* Arg = NULL);

Just to sse because I can''t create it without a valid CArg pointer.

it compiles when I call NewObject it crashes, with the debugger I saw it
seemed to call the constructor with the correct arguments first, and then
call it again with no arguments (which in this case makes my program crash), but I don''t understant why.

If someon ecould explain what I don''t understand here....
I don''t understand the behavior you described, but I''ll explain what the
behavior should be.

ObjectList[key] = CObject(Name, Arg);

The line above creates a default-constructed CObject and attempts to insert
it into the std::map as the value mapped to the key. If there is already a
value mapped to the key, then the std::map is left unchanged. The
std::map::operator[] then returns a reference to the CObject instance mapped
to key (could be the default-constructed object I just mentioned or the one
that was already present). Then, CObject''s assignment operator is invoked,
effectively replacing the contents of the instance stored inside the
std::map.

If you don''t want to have to specify a default constructor, you can use
std::map::insert instead of operator[]. That function returns an iterator
and a boolean, so you can check to see if the insert did not insert a new
key-value pair into the map and then use the iterator to modify the existing
value in that case.
An other quick question, does &ObjectList[ Name] return NULL if Name does
not match a key in the map ?




不,在这种情况下,operator []会在地图中插入一个新的键值对,并且

你获取指向默认构造的CObject实例的指针。


-

David Hilsee



No, in that case, operator[] inserts a new key-value pair into the map and
you get a pointer to the default-constructed CObject instance.

--
David Hilsee


David Hilsee <哒************* @ yahoo.com>在消息中写道

新闻:KP ******************** @ comcast.com ...

< ; snip>
"David Hilsee" <da*************@yahoo.com> wrote in message
news:KP********************@comcast.com...
<snip>
to key(可能是我刚刚提到的默认构造对象或者已经存在的
)。然后,CObject'的赋值运算符是
调用,


那应该是可以是的副本。


< snip>不,在这种情况下,operator []在地图中插入一个新的键值对,并且你得到一个指向默认构造的CObject实例的指针。
to key (could be the default-constructed object I just mentioned or the one that was already present). Then, CObject''s assignment operator is invoked,

That should read "could be a copy of the".

<snip> No, in that case, operator[] inserts a new key-value pair into the map and
you get a pointer to the default-constructed CObject instance.




再次,这应该读作复制品。


-

David Hilsee



Again, that should read "to a copy of the".

--
David Hilsee


这篇关于std :: map问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆