473,396 Members | 2,050 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,396 software developers and data experts.

how to customize compare function of std map class?

key_compare can only return true, false , but how about equal happens?

Nov 15 '06 #1
5 4067
"thinktwice" <me*********@gmail.comwrote in news:1163564035.745068.57360
@k70g2000cwa.googlegroups.com:
key_compare can only return true, false , but how about equal happens?

key_compare only needs to define a strict weak ordering.

If a < b, key_compare(a, b) returns true, key_compare(b, a) returns false.

If b < a, key_compare(a, b) returns false, key_compare(b, a) returns true.

If a == b, key_compare(a, b) returns false, key_compare(b, a) returns
false.

Nov 15 '06 #2
* thinktwice:
key_compare can only return true, false , but how about equal happens?
key_compare compares two keys a and b as if via "a < b" with an
operator< defined for the key type, so equal keys will/should yield false.

a < b <= b a
!(a < b) <= a >= b
b < a <= a b
!(b < a) <= b >= a
a == b <= (a >= b) && (b >= a) <= !(a < b) && !(b < a)

And applying De Morgan's theorem,

a == b <= !(a < b) && !(b < a) <= !((a < b) || (b < a))

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 15 '06 #3
thanks, i have customize the compare function, but seems it doesn't
work. is there anything wrong?

struct myVariantCompare{
bool operator()(VARIANT v1, VARIANT v2)
{
if (v1.vt == v2.vt)
{
if (v1.vt == VT_UNKNOWN)
{
CComPtr<IUnknownspUnk1(v1.punkVal);
if (spUnk1.IsEqualObject(v2.punkVal))
{
return false;
}
else
{
return v1.punkVal < v2.punkVal;
}
}
else
{
//assert, don't support other types
}
}
else
{
return v1.vt < v2.vt;
}
}
};
typedef std::multimap<CComVariant, long , _myVariantComparemyMap;
typedef myMap::iterator myIter;
myMap map0;
....
myIter iter = map0.find(...); //iter doesn't equal map0.end() if
there's any item matches.

is there any problem with my compare function?

Nov 15 '06 #4
thanks, i have customize the compare function, but seems it doesn't
work. is there anything wrong?

struct myVariantCompare{
bool operator()(VARIANT v1, VARIANT v2)
{
if (v1.vt == v2.vt)
{
if (v1.vt == VT_UNKNOWN)
{
CComPtr<IUnknownspUnk1(v1.punkVal);
if (spUnk1.IsEqualObject(v2.punkVal))
{
return false;
}
else
{
return v1.punkVal < v2.punkVal;
}
}
else
{
//assert, don't support other types
}
}
else
{
return v1.vt < v2.vt;
}
}
};
typedef std::multimap<CComVariant, long , _myVariantComparemyMap;
typedef myMap::iterator myIter;
myMap map0;

is there any problem with my compare function?

Nov 15 '06 #5

thinktwice wrote:
thanks, i have customize the compare function, but seems it doesn't
work. is there anything wrong?

struct myVariantCompare{
inherit from binary_function
bool operator()(VARIANT v1, VARIANT v2)
this should be const
{
if (v1.vt == v2.vt)
{
if (v1.vt == VT_UNKNOWN)
{
why not assert here instead and loose the branch?
CComPtr<IUnknownspUnk1(v1.punkVal);
if (spUnk1.IsEqualObject(v2.punkVal))
{
return false;
}
else
{
return v1.punkVal < v2.punkVal;
there is obviously a '<' operator for VARIANT::punkVal
therefore there is an equivalent for '=' operator too
so the spUnk1 seems superfluous.

I don't know COM and have no idea why you write it this way
}
}
else
{
//assert, don't support other types
this branch doesn't return
}
}
else
{
return v1.vt < v2.vt;
}
}
};
typedef std::multimap<CComVariant, long , _myVariantComparemyMap;
I suppose CComVariant and VARIANT are the same type
typedef myMap::iterator myIter;
myMap map0;

is there any problem with my compare function?
to simplify:
change VARIANT with pair

bool operator()(pair const & v1, pair const & v2) const
{
if(v1.first == v2.first)
{
if(v1.second == v2.second)
return false;
else
return v1.second < v2.second;
}
else
return v1.first < v2.first;
}

Nov 15 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: laniik | last post by:
Hi. I have a problem using STL's built in sort that seems impossible to get around. if i have: -------------------------------- struct object { int val; }
14
by: deko | last post by:
The below code dials a phone number when the subform datasheet cell containing the number is double clicked. The problem is that the dialer application (c:\windows\dialer.exe) pops up windows on...
3
by: Sam | last post by:
Hi Everyone, I have a stucture below stored in an arraylist and I want to check user's input (point x,y) to make sure there is no duplicate point x,y entered (string label can be duplicated). Is...
1
by: Linda | last post by:
Hi, Is there a way to do a "text" (rather than "binary") compareison with the "like" operator, without changing the global "Option Compare" setting? I don't want to risk breaking many, many...
9
by: Martoon | last post by:
I want to instantiate an STL map with my own compare function, and I want to pass a parameter to the compare function that will be stored and used for all comparisons in that map instance. As an...
2
by: Peter Proost | last post by:
Hi group, I want to compare path strings in order to sort them, assuming I have got: "a.txt" "dir1\c.txt" "e.txt" "dir1\d.txt" When I compare them using "e.text" would be greater than...
5
by: S S | last post by:
Hi I have a requirement where I am declaring a map within a class. class abc { map <void*, void*mMap; // I do not pass compare struct here. .... }; Here I am not passing compare function,...
1
by: Lambda | last post by:
I defined a class: class inverted_index { private: std::map<std::string, std::vector<size_t index; public: std::vector<size_tintersect(const std::vector<std::string>&); };
5
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I have a Container that is an an Array List of Class Each ArrayList element can be the class or a another ArrayList of Class So there the ArrayList could look like Element 1 - Class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.