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

how to customize compare function of std map class?

P: n/a
key_compare can only return true, false , but how about equal happens?

Nov 15 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"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

P: n/a
* 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

P: n/a
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

P: n/a
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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.