char* and STL map

B

Bart Kevelham

Hi there,

due to some compatibility issues I use char* for strings. I want to use an
STL map where I use these char* as key. But somehow this doesn't seem to
work. The first time I add something to the map, everything works fine. But
the second time, I check if something is already in the map using:

if(mapname.find(aString) != mapname.end()){
//routine for handeling the situation where there already is an object
with the key aString
}

but everytime I want to add a second element to the map with a string I
haven't used, find() still says that there already is an object with the
given key. And if I use a mapname.count(aString) it indeed gives a number
unequal to zero.

I have checked with my debugger that I indeed use another string and not the
same string as the same time. So since I don't use the same string twice, I
don't understand why things don't work....

Is it perhaps that an stl map can't handle char*? Should I convert to
std::string?

Any ideas? I use dev-c++ by the way.

Greetz, Bart.
 
W

WW

Bart said:
Hi there,

due to some compatibility issues I use char* for strings. I want to
use an STL map where I use these char* as key. But somehow this
doesn't seem to work. The first time I add something to the map,
everything works fine. But the second time, I check if something is
already in the map using:

Because you have indexed you map on the pointer value instead of comparing
the zero terminated character arrays. You will need to use a special
comparator for your map.
 
K

Kris Wempa

Are you using a local variable for a "buffer" to write your strings to, then
it always has the same address and after your first insertion, it will be a
duplicate. You can either malloc()/free() each time for a clean buffer
space or change the map to <string,string>.
 
J

Jerry Coffin

[ ... ]
due to some compatibility issues I use char* for strings. I want to use an
STL map where I use these char* as key. But somehow this doesn't seem to
work. The first time I add something to the map, everything works fine. But
the second time, I check if something is already in the map using:

if(mapname.find(aString) != mapname.end()){
//routine for handeling the situation where there already is an object
with the key aString
}

but everytime I want to add a second element to the map with a string I
haven't used, find() still says that there already is an object with the
given key. And if I use a mapname.count(aString) it indeed gives a number
unequal to zero.

By default, std::map will use operator< to compare keys. When you're
using raw pointers, that won't (normally) produce useful results.

Your options are to switch to some class (such as std::string) that
overloads operator< to do something useful, or else to supply the third
parameter when you instantiate std::map:

struct cmp_str {
bool operator()(char const *a, char const *b) {
return std::strcmp(a, b) < 0;
}
};

std::map<char *, int, cmp_str> mapname;

I'd give serious thought to switching to std::string though -- you
haven't said much specific about what compatibility issues you've faced,
but unless they're _really_ major, switching to std::string will
probably be a worthwhile investment.

On another note, I generally recommend NOT checking whether the key
exists before inserting it -- I'd normally just attempt to insert the
new item, and check the return value from the insertion to see if the
key already existed. This style of coding generally simplifies the code
and (for one example) helps avoid race conditions when/if that's an
issue.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top