Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How does the == operator for multimaps behave ?

Reply
Thread Tools

How does the == operator for multimaps behave ?

 
 
Nikhil.S.Ketkar@gmail.com
Guest
Posts: n/a
 
      08-30-2008
Hi,

How does the == operator for multimap in STL behave ? I was under the
impression that this is supposed to properly compare multimaps for
equality. It seems that what it actually does is just check if each
member in location L in one multimap is equal to the the member in
location L in the other multimap. The documentation on
http://www.sgi.com/tech/stl/Multimap.html says "Tests two multimaps
for equality. This is a global function, not a member function." and
nothing more.

Am I missing something ?
I have added an illustrative program with output.

Thanks,
Nikhil

#include <iostream>
#include <map>

int main(int argc, char *argv)
{
int numbers[] = {1,2,3,4,5};

std::multimap<int, int> a;
a.insert(std:air<int, int>(numbers[0], numbers[2]));
a.insert(std:air<int, int>(numbers[0], numbers[4]));
a.insert(std:air<int, int>(numbers[1], numbers[3]));
a.insert(std:air<int, int>(numbers[2], numbers[2]));
a.insert(std:air<int, int>(numbers[2], numbers[4]));


std::multimap<int, int> b;
b.insert(std:air<int, int>(numbers[0], numbers[2]));
b.insert(std:air<int, int>(numbers[0], numbers[4]));
b.insert(std:air<int, int>(numbers[1], numbers[3]));
b.insert(std:air<int, int>(numbers[2], numbers[2]));
b.insert(std:air<int, int>(numbers[2], numbers[4]));


std::cout << "Multimap a and b were found to be ";
if (a == b)
std::cout << "equal.\n";
else
std::cout << "unequal.\n";

std::multimap<int, int> c;
c.insert(std:air<int, int>(numbers[0], numbers[4]));
c.insert(std:air<int, int>(numbers[0], numbers[2]));
c.insert(std:air<int, int>(numbers[1], numbers[3]));
c.insert(std:air<int, int>(numbers[2], numbers[2]));
c.insert(std:air<int, int>(numbers[2], numbers[4]));

std::cout << "Multimap a and c were found to be ";
if (a == c)
std::cout << "equal.\n";
else
std::cout << "unequal.\n";

return 0;
}

// Output
//------------------------------------------------------------------------------
// Multimap a and b were found to be equal.
// Multimap a and c were found to be unequal.
//------------------------------------------------------------------------------
 
Reply With Quote
 
 
 
 
Erik Wikström
Guest
Posts: n/a
 
      08-30-2008
On 2008-08-30 06:46, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi,
>
> How does the == operator for multimap in STL behave ? I was under the
> impression that this is supposed to properly compare multimaps for
> equality. It seems that what it actually does is just check if each
> member in location L in one multimap is equal to the the member in
> location L in the other multimap.


Yes, it checks if the two multimaps are equal, and two multimaps are
equal if all the contained elements are equal. Perhaps you wanted to
check for identity, if two references/pointers to multimaps referes to
the same multimap? In that case compare the addresses of the objects
referred to.

--
Erik Wikström
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-30-2008
(E-Mail Removed) wrote:

> Hi,
>
> How does the == operator for multimap in STL behave ? I was under the
> impression that this is supposed to properly compare multimaps for
> equality.


Yes.

> It seems that what it actually does is just check if each
> member in location L in one multimap is equal to the the member in
> location L in the other multimap.


Well, that is how "equality" is defined for containers. If you think there
is a difference, maybe you want to explain what you mean by "properly
equal" (as opposed to just "equal").

Anyway, table 65 (Container requirements) defines the semantics of "==" for
all containers as

a == b
if and only if
a.size() == b.size() and equal( a.begin(), a.end(), b.begin(), b.end() )



> The documentation on
> http://www.sgi.com/tech/stl/Multimap.html says "Tests two multimaps
> for equality. This is a global function, not a member function." and
> nothing more.
>
> Am I missing something ?
> I have added an illustrative program with output.
>
> Thanks,
> Nikhil
>
> #include <iostream>
> #include <map>
>
> int main(int argc, char *argv)
> {
> int numbers[] = {1,2,3,4,5};
>
> std::multimap<int, int> a;
> a.insert(std:air<int, int>(numbers[0], numbers[2]));
> a.insert(std:air<int, int>(numbers[0], numbers[4]));
> a.insert(std:air<int, int>(numbers[1], numbers[3]));
> a.insert(std:air<int, int>(numbers[2], numbers[2]));
> a.insert(std:air<int, int>(numbers[2], numbers[4]));
>
>
> std::multimap<int, int> b;
> b.insert(std:air<int, int>(numbers[0], numbers[2]));
> b.insert(std:air<int, int>(numbers[0], numbers[4]));
> b.insert(std:air<int, int>(numbers[1], numbers[3]));
> b.insert(std:air<int, int>(numbers[2], numbers[2]));
> b.insert(std:air<int, int>(numbers[2], numbers[4]));
>
>
> std::cout << "Multimap a and b were found to be ";
> if (a == b)
> std::cout << "equal.\n";
> else
> std::cout << "unequal.\n";
>
> std::multimap<int, int> c;
> c.insert(std:air<int, int>(numbers[0], numbers[4]));
> c.insert(std:air<int, int>(numbers[0], numbers[2]));
> c.insert(std:air<int, int>(numbers[1], numbers[3]));
> c.insert(std:air<int, int>(numbers[2], numbers[2]));
> c.insert(std:air<int, int>(numbers[2], numbers[4]));
>
> std::cout << "Multimap a and c were found to be ";
> if (a == c)
> std::cout << "equal.\n";
> else
> std::cout << "unequal.\n";
>
> return 0;
> }
>
> // Output
> //------------------------------------------------------------------------------
> // Multimap a and b were found to be equal.
> // Multimap a and c were found to be unequal.
> //------------------------------------------------------------------------------


This output is correct.

BTW: what is the motivation to use such an indirect way of specifying the
elements of the maps (by using the array "numbers")?



Best

Kai-Uwe Bux
 
Reply With Quote
 
Nikhil.S.Ketkar@gmail.com
Guest
Posts: n/a
 
      08-30-2008
Thanks. That makes it quite clear.


The way I thought about it is that a multimap is a many-to-many
function and that equality of multimap implies that the functions are
identical. That is if I have two sets X = {1,2,3} and Y = {2,3,4} and
two functions from X to Y, say A = {1->2, 2->3, 3->4} and B = {1->2, 3-
>4, 2->3}, A is equal to B as the order does not matter, only the

mapping.

The example is just something I made up to make my question clear, I
was actually comparing two multimaps representing many-to-many
functions on two sets. I guess I will have to write that on my own.


Thanks again,
Nikhil

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-30-2008
(E-Mail Removed) wrote:

> Thanks. That makes it quite clear.
>
>
> The way I thought about it is that a multimap is a many-to-many
> function and that equality of multimap implies that the functions are
> identical.


That is correct from a practical point of view. How did you run into
violations of that notion?

> That is if I have two sets X = {1,2,3} and Y = {2,3,4} and
> two functions from X to Y, say A = {1->2, 2->3, 3->4} and B = {1->2, 3-
>>4, 2->3}, A is equal to B as the order does not matter, only the

> mapping.


Well, as long as you don't provide your own comparison predicate, the
multimap will be sorted in ascending order. So, in your example, B will
simply not happen since the multimap _will_ store the argument-value pairs
in the order

1->2 2->3 3->4

It will _not_ store the pairs in order of insertion.

Therefore, only the mapping matters as long as the order defaults to the one
given by std::less. Only if you provide your own comparison predicates, you
can tweak the ordering of std::multimap.



Best

Kai-Uwe Bux
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-30-2008
Kai-Uwe Bux wrote:

> (E-Mail Removed) wrote:
>
>> Thanks. That makes it quite clear.
>>
>>
>> The way I thought about it is that a multimap is a many-to-many
>> function and that equality of multimap implies that the functions are
>> identical.

>
> That is correct from a practical point of view. How did you run into
> violations of that notion?
>
>> That is if I have two sets X = {1,2,3} and Y = {2,3,4} and
>> two functions from X to Y, say A = {1->2, 2->3, 3->4} and B = {1->2, 3-
>>>4, 2->3}, A is equal to B as the order does not matter, only the

>> mapping.

>
> Well, as long as you don't provide your own comparison predicate, the
> multimap will be sorted in ascending order. So, in your example, B will
> simply not happen since the multimap _will_ store the argument-value pairs
> in the order
>
> 1->2 2->3 3->4
>
> It will _not_ store the pairs in order of insertion.
>
> Therefore, only the mapping matters as long as the order defaults to the
> one given by std::less. Only if you provide your own comparison
> predicates, you can tweak the ordering of std::multimap.


Oops: correction.

The multimap is only sorted by keys. If you use the same key twice, then the
order of insertion does matter. Sorry for the confusion. The example drove
me in that direction.


Anyway, as a fix, you could use:

std::map< key_type, std::set< mapped_type > >

which models multivalued functions as set-valued functions.


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
maps turn into multimaps brad C++ 14 08-27-2008 09:51 AM
find_if algorithm on multimaps devel C++ 3 04-10-2006 02:40 AM
How does static objects behave in ASP.NET Michael ASP .Net 4 08-04-2005 07:19 PM
Does assignment operator have to behave exactly like copy constructor bluekite2000@gmail.com C++ 12 06-16-2005 09:44 PM



Advertisments