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

H

Herby

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

Herby

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;
}

};

Thanks mate. Excellent works!!!!!
 

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,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top