Re: STL map and char * problems
Digital Puer wrote:
I am having a problem with the STL map and char *.
I'm on Linux and am using g++ 4.1.2.
I am using two STL maps:
1. map<char *, int>
1. You do not store strings in the map key here.
You store pointers to strings in the map key.
2. You ordered the entries by the pointers.
This obviously does not what you expect.
You could use a custom comparer, but it probably won't help.
(see below)
3. You have a very high risk of undefined behavior
because you can not easily control the storage
behind your char* pointers.
The pointers may point to temporaries on the stack
causing dangling references.
The storage they point to may change while the map exists.
The will change your keys by the back door.
So if you have a custom comparer it would most likely
violate the Strict Weak Ordering requirement in this case.
4. DO NOT USE char* IN C++ CODE.
It is almost always at least a risk.
Use const char* or std::string.
See example: http://www.sgi.com/tech/stl/Map.html
But this only works as long as your strings are located in static
constant storage.
Marcel