map problem

W

William Payne

Hello, I have a std::map<const char*, int> in a class and in the constructor
of the class (there is only one), I do the following initilization of the
map:
KeyMap::KeyMap()
{
keymap["F1"] = VK_F1;
keymap["F2"] = VK_F2;
keymap["F3"] = VK_F3;
keymap["F4"] = VK_F4;
keymap["F5"] = VK_F5;
keymap["F6"] = VK_F6;
keymap["F7"] = VK_F7;
keymap["F8"] = VK_F8;
keymap["F9"] = VK_F9;
keymap["F10"] = VK_F10;
keymap["F11"] = VK_F11;
keymap["F12"] = VK_F12;
keymap["Page Up"] = VK_PRIOR;
keymap["Page Down"] = VK_NEXT;
keymap["Insert"] = VK_INSERT;
keymap["Home"] = VK_HOME;
keymap["Delete"] = VK_DELETE;
keymap["End"] = VK_END;
keymap["Divide"] = VK_DIVIDE;
keymap["Multiply"] = VK_MULTIPLY;
keymap["Subtract"] = VK_SUBTRACT;
keymap["Add"] = VK_ADD;
keymap["Decimal Point"] = VK_DECIMAL;
}

But when I call the following function (get_key_code) with the string F1, an
exception is thrown
because the key is not present in the map.

int KeyMap::get_key_code(const char* key)
{
if(keymap.count(key))
{
return keymap[key];
}

throw keymap_error("No such key exists in the map.");
}

Why? And how to remedy it?

// William Payne
 
J

John Harrison

William Payne said:
Hello, I have a std::map<const char*, int> in a class and in the constructor
of the class (there is only one), I do the following initilization of the
map:
KeyMap::KeyMap()
{
keymap["F1"] = VK_F1;
keymap["F2"] = VK_F2;
keymap["F3"] = VK_F3;
keymap["F4"] = VK_F4;
keymap["F5"] = VK_F5;
keymap["F6"] = VK_F6;
keymap["F7"] = VK_F7;
keymap["F8"] = VK_F8;
keymap["F9"] = VK_F9;
keymap["F10"] = VK_F10;
keymap["F11"] = VK_F11;
keymap["F12"] = VK_F12;
keymap["Page Up"] = VK_PRIOR;
keymap["Page Down"] = VK_NEXT;
keymap["Insert"] = VK_INSERT;
keymap["Home"] = VK_HOME;
keymap["Delete"] = VK_DELETE;
keymap["End"] = VK_END;
keymap["Divide"] = VK_DIVIDE;
keymap["Multiply"] = VK_MULTIPLY;
keymap["Subtract"] = VK_SUBTRACT;
keymap["Add"] = VK_ADD;
keymap["Decimal Point"] = VK_DECIMAL;
}

But when I call the following function (get_key_code) with the string F1, an
exception is thrown
because the key is not present in the map.

int KeyMap::get_key_code(const char* key)
{
if(keymap.count(key))
{
return keymap[key];
}

throw keymap_error("No such key exists in the map.");
}

Why? And how to remedy it?

Strangely I had this exact same problem this morning.

The problem is that your map is comparing pointers not strings. Either
change your map to

std::map<std::string, int>

or add a C string comparison object

struct cstring_less_than : std::binary_function<const char*, const char*,
bool)
{
bool operator()(const char* lhs, const char* rhs) const
{
return strcmp(lhs, rhs) < 0;
}
};

std::map said:
// William Payne

john
 
E

ES Kim

John Harrison said:
[snip]

Strangely I had this exact same problem this morning.

The problem is that your map is comparing pointers not strings. Either
change your map to

std::map<std::string, int>

or add a C string comparison object

struct cstring_less_than : std::binary_function<const char*, const char*,
bool)
{
bool operator()(const char* lhs, const char* rhs) const
{
return strcmp(lhs, rhs) < 0;
}
};

std::map<std::string, int, cstring_less_than>
 
W

William Payne

John Harrison said:
William Payne said:
Hello, I have a std::map<const char*, int> in a class and in the constructor
of the class (there is only one), I do the following initilization of the
map:
KeyMap::KeyMap()
{
keymap["F1"] = VK_F1;
keymap["F2"] = VK_F2;
keymap["F3"] = VK_F3;
keymap["F4"] = VK_F4;
keymap["F5"] = VK_F5;
keymap["F6"] = VK_F6;
keymap["F7"] = VK_F7;
keymap["F8"] = VK_F8;
keymap["F9"] = VK_F9;
keymap["F10"] = VK_F10;
keymap["F11"] = VK_F11;
keymap["F12"] = VK_F12;
keymap["Page Up"] = VK_PRIOR;
keymap["Page Down"] = VK_NEXT;
keymap["Insert"] = VK_INSERT;
keymap["Home"] = VK_HOME;
keymap["Delete"] = VK_DELETE;
keymap["End"] = VK_END;
keymap["Divide"] = VK_DIVIDE;
keymap["Multiply"] = VK_MULTIPLY;
keymap["Subtract"] = VK_SUBTRACT;
keymap["Add"] = VK_ADD;
keymap["Decimal Point"] = VK_DECIMAL;
}

But when I call the following function (get_key_code) with the string
F1,
an
exception is thrown
because the key is not present in the map.

int KeyMap::get_key_code(const char* key)
{
if(keymap.count(key))
{
return keymap[key];
}

throw keymap_error("No such key exists in the map.");
}

Why? And how to remedy it?

Strangely I had this exact same problem this morning.

The problem is that your map is comparing pointers not strings. Either
change your map to

std::map<std::string, int>

or add a C string comparison object

struct cstring_less_than : std::binary_function<const char*, const char*,
bool)
{
bool operator()(const char* lhs, const char* rhs) const
{
return strcmp(lhs, rhs) < 0;
}
};

std::map said:
// William Payne

john

Thanks, I decided to use std::string instead as suggested.

// William Payne
 
J

John Harrison

ES Kim said:
John Harrison said:
[snip]

Strangely I had this exact same problem this morning.

The problem is that your map is comparing pointers not strings. Either
change your map to

std::map<std::string, int>

or add a C string comparison object

struct cstring_less_than : std::binary_function<const char*, const char*,
bool)
{
bool operator()(const char* lhs, const char* rhs) const
{
return strcmp(lhs, rhs) < 0;
}
};

std::map<std::string, int, cstring_less_than>

You mean std::map<const char*, int, cstring_less_than> ?

Yes I did. I also meant

struct cstring_less_than : std::binary_function<const char*, const char*,
bool>

not

struct cstring_less_than : std::binary_function<const char*, const char*,
bool)

john
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top