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

  • Thread starter Thread starter Sabrina
  • Start date Start date
S

Sabrina

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 said:
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
(e-mail address removed)
 
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!
 
Sabrina Lai said:
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
(e-mail address removed)
 
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!!
 
Sabrina Lai said:
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?
 
Back
Top