map(关联数组)丢失值? [英] map (associative array) loses values?

查看:98
本文介绍了map(关联数组)丢失值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我之前从未这样做过,所以我不知道任何布局

的可能性。我的道歉:)


问题是:


我写了一个函数:


map< const char *,int * SearchText :: countWords()

{

map< const char *,int * table = new map< const char * ,int> ;;


(* table)[" aap"] = 1;

(* table)[" noot"] = 2;


cout<< (* table)[" aap"]<< endl;

cout<< (* table)[" noot"]<< endl;


返回表;

}


我想这样使用:


尝试{

SearchText * text = new SearchText(" test.txt");

map< const char *,int * table = text-> countWords();

cout<< (* table)[" aap"]<< endl;

cout<< (* table)[" noot"]<< endl;

}

catch(int ex){

cout<< 无法打开文件。 <<结束;

}

但是,我得到以下输出:


1

2

0

0


意思是前两个输出语句(在函数本身中)

他们的工作和后两个没有。


我想这是某种分配问题,但我能做什么

不同?我想我可以使用malloc或calloc,但是不应该用b $ b这也可以用新的吗?


BTW使表格静态在函数中没有没帮忙。

解决方案

2007-09-11 11:42:18 -0400, je ******** @ gmail.com 说:

$ b $嗨那里,


我之前从未这样做过,所以我不知道任何布局

的可能性。我的道歉:)


问题是:


我写了一个函数:


map< const char *,int * SearchText :: countWords()

{

map< const char *,int * table = new map< const char * ,int> ;;



没有理由在堆上创建这个地图。成为当地的


>

(* table)[" aap"] = 1;

(* table)[" noot"] = 2;


cout<< (* table)[" aap"]<< endl;

cout<< (* table)[" noot"]<< endl;


返回表;

}


我想这样使用:


尝试{

SearchText * text = new SearchText(" test.txt");

map< const char *,int * table = text-> countWords();

cout<< (* table)[" aap"]<< endl;

cout<< (* table)[" noot"]<< endl;

}

catch(int ex){

cout<< 无法打开文件。 <<结束;

}


但是,我得到以下输出:


1

2

0

0


含义前两个输出语句(在函数本身中)

做他们的工作而后两个没做。


我想这是某种分配问题,但我能做些什么

不同?我想我可以使用malloc或calloc,但是不应该用b $ b这也可以用新的吗?


BTW使表格静态在函数中没有没有帮助。



-

Pete

Roundhouse Consulting,Ltd。( www.versatilecoding.com )作者

标准C ++库扩展:教程和参考

www.petebecker.com/tr1book


On 2007-09-11 11:47:36 -0400,Pete Becker< pe ** @ versatilecoding.comsaid:


在2007-09-11 11:42:18 -0400, je **** **** @ gmail.com 说:


>你好,

我从来没有这样做过之前,所以我不知道任何布局
的可能性。我的道歉:)

问题是:

我写了一个函数:

map< const char *,int * SearchText :: countWords()
{
map< const char *,int * table = new map< const char *,int> ;;



没有理由在堆上创建这个地图。让它成为一个本地



忽略这一点。发布太快了。 : - (


-

Pete

Roundhouse Consulting,Ltd。( www.versatilecoding.com )作者

标准C ++库扩展:教程和参考文献

www.petebecker.com/tr1book


je ******** @ gmail。 com 写道:


你好,


我之前从未这样做过,所以我不喜欢不知道任何布局

的可能性。我的道歉:)


问题是:


我写了一个函数:


map< const char *,int * SearchText :: countWords()

{

map< const char *,int * table = new map< const char *,int> ;;


(* table)[" aap"] = 1;

(* table)[" noot"] = 2;


cout<< (* table)[" aap"]<< endl;

cout<< (* table)[" noot"]<< endl;


返回表;

}


我想这样使用:


尝试{

SearchText * text = new SearchText(" test.txt");

map< const char *,int * table = text-> countWords();

cout<< (* table)[" aap"]<< endl;

cout<< (* table)[" noot"]<< endl;

}

catch(int ex){

cout<< 无法打开文件。 <<结束;

}


但是,我得到以下输出:


1

2

0

0


含义前两个输出语句(在函数本身中)

做他们的工作而后两个没做。


我想这是某种分配问题,



nope。


问题是数据结构


map< char *,...>


请注意,这将(a)存储指向char的指针和(b)比较那些

指针(不是字符串)指出)识别钥匙。还要注意两个

不同的字符串文字aap。 _not_保证由

表示相同的char *。这就是为什么你找不到录制的条目。


但我能做些什么呢?



使用map< std :: string,...而不是。这将存储和比较值。


我想我可能会使用malloc或calloc,但不应该这也是

可能有新的吗?



关于这个注意事项:为什么要动态分配地图呢?你的代码

无缘无故地使用new()(并且它也错过了相应的

删除语句)。


BTW在函数中使表静态没有帮助。



预计不会。

最佳


Kai-Uwe Bux


Hi there,

I''ve never done this before, so I don''t know about any layout
possibilities. My apologies :)

The problem is this:

I''ve written a function:

map<const char*, int*SearchText::countWords()
{
map<const char*, int*table = new map<const char*, int>;

(*table)["aap"] = 1;
(*table)["noot"] = 2;

cout << (*table)["aap"] << endl;
cout << (*table)["noot"] << endl;

return table;
}

Which I want to use like this:

try {
SearchText *text = new SearchText("test.txt");
map<const char*, int*table = text->countWords();
cout << (*table)["aap"] << endl;
cout << (*table)["noot"] << endl;
}
catch(int ex) {
cout << "Could not open file." << endl;
}
However, I get the following output:

1
2
0
0

Meaning that the first two output statements (in the function itself)
do their job and the second two do not.

I guess it''s some sort of allocation problem, but what could I do
different? I guess I could use maybe malloc or calloc, but shouldn''t
this be also possible with new?

BTW making table static in the function didn''t help.

解决方案

On 2007-09-11 11:42:18 -0400, je********@gmail.com said:

Hi there,

I''ve never done this before, so I don''t know about any layout
possibilities. My apologies :)

The problem is this:

I''ve written a function:

map<const char*, int*SearchText::countWords()
{
map<const char*, int*table = new map<const char*, int>;

There''s no reason to create this map on the heap. Make it a local

>
(*table)["aap"] = 1;
(*table)["noot"] = 2;

cout << (*table)["aap"] << endl;
cout << (*table)["noot"] << endl;

return table;
}

Which I want to use like this:

try {
SearchText *text = new SearchText("test.txt");
map<const char*, int*table = text->countWords();
cout << (*table)["aap"] << endl;
cout << (*table)["noot"] << endl;
}
catch(int ex) {
cout << "Could not open file." << endl;
}
However, I get the following output:

1
2
0
0

Meaning that the first two output statements (in the function itself)
do their job and the second two do not.

I guess it''s some sort of allocation problem, but what could I do
different? I guess I could use maybe malloc or calloc, but shouldn''t
this be also possible with new?

BTW making table static in the function didn''t help.


--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)


On 2007-09-11 11:47:36 -0400, Pete Becker <pe**@versatilecoding.comsaid:

On 2007-09-11 11:42:18 -0400, je********@gmail.com said:

>Hi there,

I''ve never done this before, so I don''t know about any layout
possibilities. My apologies :)

The problem is this:

I''ve written a function:

map<const char*, int*SearchText::countWords()
{
map<const char*, int*table = new map<const char*, int>;


There''s no reason to create this map on the heap. Make it a local

Ignore this. Posted too soon. :-(

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)


je********@gmail.com wrote:

Hi there,

I''ve never done this before, so I don''t know about any layout
possibilities. My apologies :)

The problem is this:

I''ve written a function:

map<const char*, int*SearchText::countWords()
{
map<const char*, int*table = new map<const char*, int>;

(*table)["aap"] = 1;
(*table)["noot"] = 2;

cout << (*table)["aap"] << endl;
cout << (*table)["noot"] << endl;

return table;
}

Which I want to use like this:

try {
SearchText *text = new SearchText("test.txt");
map<const char*, int*table = text->countWords();
cout << (*table)["aap"] << endl;
cout << (*table)["noot"] << endl;
}
catch(int ex) {
cout << "Could not open file." << endl;
}
However, I get the following output:

1
2
0
0

Meaning that the first two output statements (in the function itself)
do their job and the second two do not.

I guess it''s some sort of allocation problem,

nope.

The problem is the data structure

map< char*, ... >

Note that this will (a) store pointers to char and (b) compare those
pointers (not the strings pointed to) to identify keys. Also note that two
different string literals "aap" are _not_ guaranteed to be represented by
the same char*. That is why you don''t find the recorded entries.

but what could I do different?

Use map< std::string, ... instead. That will store and compare values.

I guess I could use maybe malloc or calloc, but shouldn''t this be also
possible with new?

On that note: why do you do dynamic allocation of the map anyway? Your code
is littered with new() for no reason (and it misses the corresponding
delete statements, too).

BTW making table static in the function didn''t help.

It is not expected to.
Best

Kai-Uwe Bux


这篇关于map(关联数组)丢失值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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