473,386 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
2 3309
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Antti Granqvist | last post by:
Hello! I have following object relations: Competition 1--* Category 1--* Course 1 | | * Course
2
by: ash | last post by:
Hi, I'm getting started with STL, and am stuck at creating a map container. I checked one of the texts and found a code in there. To make it simple, i wrote the following: #include...
5
by: Peter Jansson | last post by:
Hello, I have the following code: std::map<int,std::set<std::string> > k; k="1234567890"; k="2345678901"; //... std::set<std::string> myMethod(std::map<int,std::set<std::string> > k)...
13
by: jstanforth | last post by:
This is probably a very obvious question, but I'm not clear on what operators need to be implemented for std::map.find() to work. For example, I have a class MyString that wraps std::string, and...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
1
by: Maxwell | last post by:
Hello, I having having oodles of trouble using the std lib in my MC++ (VS.NET 2003) Class library. I figured out a simple sample to reproduce the errors I am having. Create a MC++ (VS.NET 2003)...
4
by: Evyn | last post by:
Hi all, I'm starting to fool around with STL and in particular std::map. How do I iterate through one map and insert every pair in another map? I have the following so far: map<double,...
5
by: Diwa | last post by:
Does the "value" type (value as in key-value pair )of "std::map" require a default ctor even if it is not used ? If I comment out Line 1 in the code attached later, i.e remove the default ctor...
5
by: Chris Forone | last post by:
Hello group, g++ (3.4.2, mingw): float init = {1.f, 2.f, 3.f}; std::map<std::string, std::valarray<float mp; mp = std::valarray(init, 3); mp.size(); // should be 3, but IS 0!
8
by: mveygman | last post by:
Hi, I am writing code that is using std::map and having a bit of an issue with its performance. It appears that the std::map is significantly slower searching for an element then a sequential...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.