无法使hash_map与.NET中的C ++一起使用 [英] Cannot get hash_map to work with C++ in .NET

查看:63
本文介绍了无法使hash_map与.NET中的C ++一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮忙吗?我一直在尝试使用C ++ for .NET获取hash_map

来处理字符串和const char *。我使用const char *作为键和指向

另一个类作为数据类型的指针。我希望能够为每个键分配唯一的名称,并允许用户

能够搜索hash_map以找到该键,所以

做,通过另一个类''指针(数据类型)检索指向

的信息。我可以将const char *的
值硬编码到hash_map键中,并通过测试和修改源代码来检索

,但是当b $ b时我尝试允许用户输入找到一个

特定的密钥,这不起作用。 1)cin没有

允许const char *所以我必须使用字符串2)即使

我使用string str; const char * nme; nme = str.c_str();"

然后将其传递给函数find

中的密钥hash_map它不起作用。然而,如上所述,硬编码

const char *的所有值都可以。有人可以帮忙吗?我将非常感激
!谢谢!

解决方案



" Sabrina" < ZS ****** @ ieee.org> skrev i meddelandet

新闻:0b **************************** @ phx.gbl ... < blockquote class =post_quotes>有人可以帮忙吗?我一直试图在C ++ for .NET中使用hash_map来处理字符串和const char *。我使用const char *作为键和指向另一个类作为数据类型的指针。我希望能够为每个密钥分配唯一的名称,并允许用户能够搜索hash_map以找到该密钥,并在这样做,检索信息。由另一个类''指针(数据类型)指向
。我可以将const char *的值硬编码到hash_map键中并通过测试和修改源代码来检索它们,但是当我尝试允许用户输入来查找
特别关键这不起作用。 1)cin不允许const char *所以我必须使用字符串2)即使我使用string str; const char * nme; nme = str.c_str();"
然后将其传递给函数find
hash_map中的关键不起作用。然而,如上所述,硬编码const char *的所有值都将起作用。有人可以帮忙吗?我将非常感激!谢谢!




您好Sabrina!


在容器中存储指针通常不是一个好主意(除非

你有一个非常具体的需要)。在你的情况下,使用指针

作为键是不好的,因为键必须是常量或其他

容器会破坏。


当您在测试中使用字符串文字时,这将更好用

因为它们是常量并存储在程序的

持续时间的单个位置。当您从用户读取输入时,一个输入

将在读取下一个输入时消失。你的指针然后没有

更长的指向什么!


如果你想将字符串映射到某个东西,你应该真的存储

std :: string在地图中,因为这样地图本身将保留该键的

值。

Bo Persson
bo ** @ telia.com


感谢您的回复!嗯,好吧,但我没有使用指针作为

键类型。这是声明:

hash_map< const char *,StudentClass *> studentList;


在将值插入hash_map的同一函数中,你可以调用StudentClass并检索像getNumber()这样的函数。 />
查找与该指针关联的该类中的数字

StudentClass。但是,当你尝试使用string str时; cin>> str;

studentList.find(str.c_str());从一个不同的功能它不会
找到指定的字符串名称。虽然通过调试你会发现

,你想要存储在hash_map中的值仍然是

那里,也许你已经给出了其中一个你看到的名字

hash_map本身它仍然没有找到(如果hash_map返回

studentList.end())


任何想法???再次感谢!


***通过Developersdex发送 http:/ /www.developersdex.com ***

不要只是参加USENET ......获得奖励!


< blockquote>

" Sabrina Lai" < ZS ****** @ yahoo.com> skrev i meddelandet

新闻:uM ************** @ tk2msftngp13.phx.gbl ...

感谢您的回复!嗯,好吧,但是我没有使用指针作为
的关键类型。这是声明:
hash_map< const char *,StudentClass *> studentList;


我认为const char *看起来非常像一个指针类型,结束于

*和所有。这意味着你要比较

字符串的地址,而不是它们的值。


如何尝试


hash_map< std :: string,StudentClass>




一开始就意识到一个对象有一个名字,它会让人感到困惑,

地址和值。这些是对象的不同属性。

在将值插入hash_map的同一函数中,
您可以调用StudentClass并检索函数,如
getNumber()找到与该指针关联的该类中的数字
StudentClass。但是,当你尝试使用字符串str;



cin>> str;


这里还有另一个潜在的问题:cin> ;> str只从流中读取一个

字。如果你想阅读包含空格的全名

,你应该看一下getline()函数。

Bo Persson
bo ** @ telia.com


Can someone help? I have been trying to get the hash_map
in C++ for .NET to work with strings and const char*. I
am using the const char* as the key and a pointer to
another class as the data type. I would like to be able
to assign unique names to each key and allow the user to
be able to search the hash_map to find that key and in so
doing, retrieve the information that is being pointed to
by the other class'' pointer(data type). I can hard code
values of const char* into the hash_map key and retrieve
them through testing and modifying the source code,
however when I try allowing for user input to find a
particular key this does not work. 1) "cin" does not
allow for "const char*" so I must use string 2) even when
I use "string str; const char* nme; nme = str.c_str();"
and then pass this into the function to "find" the key in
the hash_map it will not work. Yet, as said, hardcoding
all values of const char* will work. Can anyone help? I
would be very grateful! Thank you!

解决方案


"Sabrina" <zs******@ieee.org> skrev i meddelandet
news:0b****************************@phx.gbl...

Can someone help? I have been trying to get the hash_map
in C++ for .NET to work with strings and const char*. I
am using the const char* as the key and a pointer to
another class as the data type. I would like to be able
to assign unique names to each key and allow the user to
be able to search the hash_map to find that key and in so
doing, retrieve the information that is being pointed to
by the other class'' pointer(data type). I can hard code
values of const char* into the hash_map key and retrieve
them through testing and modifying the source code,
however when I try allowing for user input to find a
particular key this does not work. 1) "cin" does not
allow for "const char*" so I must use string 2) even when
I use "string str; const char* nme; nme = str.c_str();"
and then pass this into the function to "find" the key in
the hash_map it will not work. Yet, as said, hardcoding
all values of const char* will work. Can anyone help? I
would be very grateful! Thank you!



Hi Sabrina!

Storing pointers in containers is generally not a good idea (unless
you have a very specific need for this). In your case, using pointers
as keys is not good, because the keys must be constant or otherwise
the container will break.

When you use string literals in your tests, that will work better
because they are constant and stored in a single place for the
duration of the program. When you read input from the user, one input
will disappear when the next input is read. Your pointer then no
longer points to anything!

If you want to map string to something, you should really store the
std::string in the map, because that way the map itself will keep the
value of the key.
Bo Persson
bo**@telia.com


Thanks for the response! Hmmm, okay, but I am not using pointers as the
key type. This is the declaration:
hash_map <const char*, StudentClass*> studentList;

Within the same function that you insert values into the hash_map, you
are able to call StudentClass and retrieve a function like getNumber()
to find the number in that class associated with that pointer to
StudentClass. However, when you are trying to use string str; cin>>str;
studentList.find(str.c_str()); from a different function it does not
find the specified string name. Although through debugging you do find
that the values you had wanted to store within the hash_map are still
there and, perhaps you have given one of those names you do see in the
hash_map itself it still returns not found(in case of hash_map returns
studentList.end())

Any thoughts??? Thanks again!

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!



"Sabrina Lai" <zs******@yahoo.com> skrev i meddelandet
news:uM**************@tk2msftngp13.phx.gbl...

Thanks for the response! Hmmm, okay, but I am not using pointers as the key type. This is the declaration:
hash_map <const char*, StudentClass*> studentList;
I think "const char*" looks very much like a pointer type, ending in a
* and all. This means that you are comparing the addresses of the
strings, and not their values.

How about trying

hash_map<std::string, StudentClass>

?
It can confusing at first to realize that an object has a name, an
address, and a value. These are distinct properties of the object.

Within the same function that you insert values into the hash_map, you are able to call StudentClass and retrieve a function like getNumber() to find the number in that class associated with that pointer to
StudentClass. However, when you are trying to use string str;


cin>>str;

There is another potential problem here: cin >> str only read a single
word from the stream. If you want to read full names potentially
containing spaces, you should look at the getline() function instead.
Bo Persson
bo**@telia.com


这篇关于无法使hash_map与.NET中的C ++一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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