Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   map problem (http://www.velocityreviews.com/forums/t277088-map-problem.html)

William Payne 08-26-2003 01:28 PM

map problem
 
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



John Harrison 08-26-2003 01:39 PM

Re: map problem
 

"William Payne" <mikas493_no_spam@student.liu.se> wrote in message
news:bifn99$iol$1@news.island.liu.se...
> 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<std::string, int, cstring_less_than>

> // William Payne
>


john



ES Kim 08-26-2003 02:21 PM

Re: map problem
 
"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:bifnse$8m1fn$1@ID-196037.news.uni-berlin.de...
>
> "William Payne" <mikas493_no_spam@student.liu.se> wrote in message
> news:bifn99$iol$1@news.island.liu.se...
>


[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> ?

>
> > // William Payne
> >

>
> john
>
>


--
ES Kim



William Payne 08-26-2003 02:33 PM

Re: map problem
 

"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:bifnse$8m1fn$1@ID-196037.news.uni-berlin.de...
>
> "William Payne" <mikas493_no_spam@student.liu.se> wrote in message
> news:bifn99$iol$1@news.island.liu.se...
> > 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<std::string, int, cstring_less_than>
>
> > // William Payne
> >

>
> john
>
>


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

// William Payne



John Harrison 08-26-2003 03:07 PM

Re: map problem
 

"ES Kim" <eskim@svd.co.kr> wrote in message
news:bifo5c$r24$1@news1.kornet.net...
> "John Harrison" <john_andronicus@hotmail.com> wrote in message
> news:bifnse$8m1fn$1@ID-196037.news.uni-berlin.de...
> >
> > "William Payne" <mikas493_no_spam@student.liu.se> wrote in message
> > news:bifn99$iol$1@news.island.liu.se...
> >

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




All times are GMT. The time now is 07:14 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.