Connecting Tech Pros Worldwide Help | Site Map

how to customize compare function of std map class?

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 15th, 2006, 02:55 AM
thinktwice
Guest
 
Posts: n/a
Default how to customize compare function of std map class?

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


  #2  
Old November 15th, 2006, 03:05 AM
Andre Kostur
Guest
 
Posts: n/a
Default Re: how to customize compare function of std map class?

"thinktwice" <memorialday@gmail.comwrote in news:1163564035.745068.57360
@k70g2000cwa.googlegroups.com:
Quote:
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.

  #3  
Old November 15th, 2006, 03:15 AM
Alf P. Steinbach
Guest
 
Posts: n/a
Default Re: how to customize compare function of std map class?

* thinktwice:
Quote:
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?
  #4  
Old November 15th, 2006, 05:35 AM
thinktwice
Guest
 
Posts: n/a
Default Re: how to customize compare function of std map class?

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?

  #5  
Old November 15th, 2006, 05:55 AM
thinktwice
Guest
 
Posts: n/a
Default Re: how to customize compare function of std map class?

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?

  #6  
Old November 15th, 2006, 12:15 PM
dasjotre
Guest
 
Posts: n/a
Default Re: how to customize compare function of std map class?


thinktwice wrote:
Quote:
thanks, i have customize the compare function, but seems it doesn't
work. is there anything wrong?
>
struct myVariantCompare{
inherit from binary_function
Quote:
bool operator()(VARIANT v1, VARIANT v2)
this should be const
Quote:
{
if (v1.vt == v2.vt)
{
if (v1.vt == VT_UNKNOWN)
{
why not assert here instead and loose the branch?
Quote:
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
Quote:
}
}
else
{
//assert, don't support other types
this branch doesn't return
Quote:
}
}
else
{
return v1.vt < v2.vt;
}
}
>
>
};
>
>
typedef std::multimap<CComVariant, long , _myVariantComparemyMap;
I suppose CComVariant and VARIANT are the same type
Quote:
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;
}

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.