Connecting Tech Pros Worldwide Forums | Help | Site Map

char* and STL map

Bart Kevelham
Guest
 
Posts: n/a
#1: Jul 19 '05
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.






WW
Guest
 
Posts: n/a
#2: Jul 19 '05

re: char* and STL map


Bart Kevelham wrote:[color=blue]
> 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:[/color]

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.

--
WW aka Attila


Kris Wempa
Guest
 
Posts: n/a
#3: Jul 19 '05

re: char* and STL map


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>.

"Bart Kevelham" <b.kevelham@student.utwente.nl> wrote in message
news:blsbnh$e69$2@netlx020.civ.utwente.nl...[color=blue]
> 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.[/color]
But[color=blue]
> 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[/color]
the[color=blue]
> same string as the same time. So since I don't use the same string twice,[/color]
I[color=blue]
> 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.
>
>
>
>
>[/color]


Jerry Coffin
Guest
 
Posts: n/a
#4: Jul 19 '05

re: char* and STL map


In article <blsbnh$e69$2@netlx020.civ.utwente.nl>,
b.kevelham@student.utwente.nl says...

[ ... ]
[color=blue]
> 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.[/color]

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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Closed Thread