By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,428 Members | 1,845 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,428 IT Pros & Developers. It's quick & easy.

How to write a multi value compare function for std::map

P: n/a
Hi,

My key for the map is a class with two strings, group and name as
follows:

class ConfigKey{
public:

ConfigKey( String group, String key );

String mGroup;
String mName;
};
Now I want to do a lookup on both values matching.

So I have defined my own compare functions as follows:

struct cmp_config_key
{
bool operator()(ConfigKey const *lhs, ConfigKey const *rhs)
{
if( lhs->mGroup < rhs->mGroup ){
if( lhs->mKey < rhs->mKey )
return true;
}

return false;

}
};

Above is not working correctly as it returns some other key when i
call find. Im not too clear on this weak ordering business and it
gets even more confusing with multiple values acting as the key.

Naturally and intuitively i just want to write

if( lhs->mGroup == rhs->mGroup ){
if( lhs->mKey == rhs->mKey )
return true;
}

Please could someone correct my function???
Nov 10 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Mon, 10 Nov 2008 09:22:21 -0800, Herby wrote:
Hi,

My key for the map is a class with two strings, group and name as
follows:

class ConfigKey{
public:

ConfigKey( String group, String key );

String mGroup;
String mName;
};
Now I want to do a lookup on both values matching.

So I have defined my own compare functions as follows:

struct cmp_config_key
{
bool operator()(ConfigKey const *lhs, ConfigKey const *rhs) {
if( lhs->mGroup < rhs->mGroup ){
if( lhs->mKey < rhs->mKey )
return true;
}

return false;

}
};

Above is not working correctly as it returns some other key when i call
find. Im not too clear on this weak ordering business and it gets even
more confusing with multiple values acting as the key.

Naturally and intuitively i just want to write

if( lhs->mGroup == rhs->mGroup ){
if( lhs->mKey == rhs->mKey )
return true;
}

Please could someone correct my function???
struct cmp_config_key
{
bool operator()(ConfigKey const *lhs, ConfigKey const *rhs) const
{
if(lhs->mGroup < rhs->mGroup) {
return true;
}
if(lhs->mGroup == rhs->mGroup) {
if(lhs->mKey < rhs->mKey) {
return true;
}
}
return false;
}
};

--
OU
Remember 18th of June 2008, Democracy died that afternoon.
http://frapedia.se/wiki/Information_in_English
Nov 10 '08 #2

P: n/a
On Nov 10, 5:58 pm, Obnoxious User <O...@127.0.0.1wrote:
On Mon, 10 Nov 2008 09:22:21 -0800, Herby wrote:
Hi,
My key for the map is a class with two strings, group and name as
follows:
class ConfigKey{
public:
ConfigKey( String group, String key );
String mGroup;
String mName;
};
Now I want to do a lookup on both values matching.
So I have defined my own compare functions as follows:
struct cmp_config_key
{
bool operator()(ConfigKey const *lhs, ConfigKey const *rhs) {
if( lhs->mGroup < rhs->mGroup ){
if( lhs->mKey < rhs->mKey )
return true;
}
return false;
}
};
Above is not working correctly as it returns some other key when i call
find. Im not too clear on this weak ordering business and it gets even
more confusing with multiple values acting as the key.
Naturally and intuitively i just want to write
if( lhs->mGroup == rhs->mGroup ){
if( lhs->mKey == rhs->mKey )
return true;
}
Please could someone correct my function???

struct cmp_config_key
{
bool operator()(ConfigKey const *lhs, ConfigKey const *rhs) const
{
if(lhs->mGroup < rhs->mGroup) {
return true;
}
if(lhs->mGroup == rhs->mGroup) {
if(lhs->mKey < rhs->mKey) {
return true;
}
}
return false;
}

};

--
OU
Remember 18th of June 2008, Democracy died that afternoon.http://frapedia.se/wiki/Information_in_English
Thanks mate. Excellent works!!!!!

Nov 10 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.