473,395 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Cannot get hash_map to work with C++ in .NET

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!
Jul 19 '05 #1
5 4545

"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

Jul 19 '05 #2
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!
Jul 19 '05 #3

"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

Jul 19 '05 #4
I see what you mean about the const char*, my confusion, however it is
interesting that the const char* does work without flaw when hardcoding
all values. Such as their set values and their "find" values.
const char* student1 = "Student";
StudentClass* pSc;
studentList[student1] = pSc;
//from another function- studentName is a const char* passed in with
value of "Student"
studentList.find(studentName);
//returns the correct values

Also, on the Microsoft Visual Studio .NET 2002 version in C++ hash_map
strings will not work at all. It compiles with an error when you try to
use hash_map <string, studentclass*>

I do not mean to be objectionable but this is what has happened over a
month of my work with these hash_maps under Microsoft Visual STudio .Net
It also upsets me because we had bought the .NET solely for its hash_map
capabilities. If it helps any we need the hash_maps here to work the
same as in LISP.
Thanks again!!

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

"Sabrina Lai" <zs******@yahoo.com> skrev i meddelandet
news:%2******************@tk2msftngp13.phx.gbl...
I see what you mean about the const char*, my confusion, however it is interesting that the const char* does work without flaw when hardcoding all values. Such as their set values and their "find" values.
const char* student1 = "Student";
Yes, the literal "Student" is a constant that he compiler puts
somewhere in memory. Its address will not change during the program.
StudentClass* pSc;
studentList[student1] = pSc;
//from another function- studentName is a const char* passed in with
value of "Student"
studentList.find(studentName);
//returns the correct values

Also, on the Microsoft Visual Studio .NET 2002 version in C++ hash_map strings will not work at all. It compiles with an error when you try to use hash_map <string, studentclass*>
I can get it to compile. What kind of errors do you get?

I do not mean to be objectionable but this is what has happened over a month of my work with these hash_maps under Microsoft Visual STudio ..Net It also upsets me because we had bought the .NET solely for its hash_map capabilities. If it helps any we need the hash_maps here to work the
same as in LISP.
Thanks again!!


Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Jon Cosby | last post by:
I need help in hashmaps. Why doesn't this work: #include <hash_map> hash_map <int, string> hm1; typedef pair <int, string> pr; hm1.insert(str_pair(1, "Hello")); It compiles, but crashes at...
0
by: Jianli Shen | last post by:
#include <ext/hash_map> typedef HASH_MAP<Addr1, Addr2> AddrPair; typedef HASH_MAP<AddrPair, int> PairCount; typedef HASH_MAP<Addr1, PairCount> PairMap; AddrPair addrPair; PairCount...
5
by: Joseph Turian | last post by:
In a class templatized by class T, I have the following two lines: hash_map<string, T> foo; hash_map<string, T>::const_iterator p; The first line compiles just fine. The second line gives the...
15
by: Sabrina | last post by:
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...
1
by: jayesah | last post by:
Hi All, I am developing my code with Apache stdcxx. I am bound to use STL of Apache only. Now today I need hash_map in code but as I learned, it is not available in Apache since it is not...
11
by: xz | last post by:
>From the reference of MSDN about hash map: operator Inserts an element into a hash_map with a specified key value. And such example is given: hash_map <int, inthm1; hm1 = 40;
2
by: Amit Bhatia | last post by:
Hi, I am trying to use hash maps from STL on gcc 3.3 as follows: #ifndef NODE_H #define NODE_H #include <ext/hash_map> #include "node_hasher.h" class Node; typedef hash_map<pair<int,int>,...
1
by: Mirco Wahab | last post by:
Alf P. Steinbach wrote: No, there isn't any (afaik). You can look it up here: http://www.boost.org/doc/libs/1_35_0/doc/html/boost_tr1/unsupported.html#boost_tr1.unsupported.unordered_map ...
2
by: marek.vondrak | last post by:
Hi, I am wondering if there are any functional differences between SGI's hash_map and tr1's unordered_map. Can these two containers be interchanged? What would it take to switch from hash_map to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.