Connecting Tech Pros Worldwide Help | Site Map

How does the == operator for multimaps behave ?

Nikhil.S.Ketkar@gmail.com
Guest
 
Posts: n/a
#1: Aug 30 '08
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, inta;
a.insert(std::pair<int, int>(numbers[0], numbers[2]));
a.insert(std::pair<int, int>(numbers[0], numbers[4]));
a.insert(std::pair<int, int>(numbers[1], numbers[3]));
a.insert(std::pair<int, int>(numbers[2], numbers[2]));
a.insert(std::pair<int, int>(numbers[2], numbers[4]));


std::multimap<int, intb;
b.insert(std::pair<int, int>(numbers[0], numbers[2]));
b.insert(std::pair<int, int>(numbers[0], numbers[4]));
b.insert(std::pair<int, int>(numbers[1], numbers[3]));
b.insert(std::pair<int, int>(numbers[2], numbers[2]));
b.insert(std::pair<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, intc;
c.insert(std::pair<int, int>(numbers[0], numbers[4]));
c.insert(std::pair<int, int>(numbers[0], numbers[2]));
c.insert(std::pair<int, int>(numbers[1], numbers[3]));
c.insert(std::pair<int, int>(numbers[2], numbers[2]));
c.insert(std::pair<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.
//------------------------------------------------------------------------------
Kai-Uwe Bux
Guest
 
Posts: n/a
#2: Aug 30 '08

re: How does the == operator for multimaps behave ?


Nikhil.S.Ketkar@gmail.com wrote:
Quote:
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.
Quote:
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() )


Quote:
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, inta;
a.insert(std::pair<int, int>(numbers[0], numbers[2]));
a.insert(std::pair<int, int>(numbers[0], numbers[4]));
a.insert(std::pair<int, int>(numbers[1], numbers[3]));
a.insert(std::pair<int, int>(numbers[2], numbers[2]));
a.insert(std::pair<int, int>(numbers[2], numbers[4]));
>
>
std::multimap<int, intb;
b.insert(std::pair<int, int>(numbers[0], numbers[2]));
b.insert(std::pair<int, int>(numbers[0], numbers[4]));
b.insert(std::pair<int, int>(numbers[1], numbers[3]));
b.insert(std::pair<int, int>(numbers[2], numbers[2]));
b.insert(std::pair<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, intc;
c.insert(std::pair<int, int>(numbers[0], numbers[4]));
c.insert(std::pair<int, int>(numbers[0], numbers[2]));
c.insert(std::pair<int, int>(numbers[1], numbers[3]));
c.insert(std::pair<int, int>(numbers[2], numbers[2]));
c.insert(std::pair<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
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
 
Posts: n/a
#3: Aug 30 '08

re: How does the == operator for multimaps behave ?


On 2008-08-30 06:46, Nikhil.S.Ketkar@gmail.com wrote:
Quote:
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
Closed Thread