D
Digital Puer
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>
2. map<string, int>
For some reason, when I insert the same char strings and
then iterate over them, the map from (1) will not print the
keys in the expected lexical (alphabetical) order, but the
map from (2) will print the keys in order. I would prefer
to use map (1).
Here is a program that demonstrates this problem.
map<char *, int> keyCountsChar;
keyCountsChar["bbtman"] = 1;
keyCountsChar["batman"] = 1;
keyCountsChar["aorta"] = 1;
keyCountsChar["dude"] = 1;
printf("content of keyCountsChar:\n");
for (map<char *, int>::iterator iter = keyCountsChar.begin();
iter != keyCountsChar.end();
iter++)
{
printf("%s --> %d\n", iter->first, iter->second);
}
map<string, int> keyCountsString;
keyCountsString["bbtman"] = 1;
keyCountsString["batman"] = 1;
keyCountsString["aorta"] = 1;
keyCountsString["dude"] = 1;
printf("content of keyCountsString:\n");
for (map<string, int>::iterator iter = keyCountsString.begin();
iter != keyCountsString.end();
iter++)
{
printf("%s --> %d\n", iter->first.c_str(), iter->second);
}
The output is:
content of keyCountsChar:
bbtman --> 1
batman --> 1
aorta --> 1
dude --> 1
content of keyCountsString:
aorta --> 1
batman --> 1
bbtman --> 1
dude --> 1
As you can see, the map with the string iterates over the
keys in the correct lexical order, but the maps with the char *
does not.
Can someone please help me out? I would like to use
the map with the char * and still iterate in lexical order.
I'm on Linux and am using g++ 4.1.2.
I am using two STL maps:
1. map<char *, int>
2. map<string, int>
For some reason, when I insert the same char strings and
then iterate over them, the map from (1) will not print the
keys in the expected lexical (alphabetical) order, but the
map from (2) will print the keys in order. I would prefer
to use map (1).
Here is a program that demonstrates this problem.
map<char *, int> keyCountsChar;
keyCountsChar["bbtman"] = 1;
keyCountsChar["batman"] = 1;
keyCountsChar["aorta"] = 1;
keyCountsChar["dude"] = 1;
printf("content of keyCountsChar:\n");
for (map<char *, int>::iterator iter = keyCountsChar.begin();
iter != keyCountsChar.end();
iter++)
{
printf("%s --> %d\n", iter->first, iter->second);
}
map<string, int> keyCountsString;
keyCountsString["bbtman"] = 1;
keyCountsString["batman"] = 1;
keyCountsString["aorta"] = 1;
keyCountsString["dude"] = 1;
printf("content of keyCountsString:\n");
for (map<string, int>::iterator iter = keyCountsString.begin();
iter != keyCountsString.end();
iter++)
{
printf("%s --> %d\n", iter->first.c_str(), iter->second);
}
The output is:
content of keyCountsChar:
bbtman --> 1
batman --> 1
aorta --> 1
dude --> 1
content of keyCountsString:
aorta --> 1
batman --> 1
bbtman --> 1
dude --> 1
As you can see, the map with the string iterates over the
keys in the correct lexical order, but the maps with the char *
does not.
Can someone please help me out? I would like to use
the map with the char * and still iterate in lexical order.