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

How does the == operator for multimaps behave ?

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.
//------------------------------------------------------------------------------
Aug 30 '08 #1
2 1524
Ni*************@gmail.com 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, 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
Aug 30 '08 #2
On 2008-08-30 06:46, Ni*************@gmail.com 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
Aug 30 '08 #3

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

Similar topics

12
by: c++novice | last post by:
1--Can operators be virtual? 2--What is the difference between an operator= returning a refernce Vs a value?
4
by: Mark Stijnman | last post by:
A while ago I posted a question about how to get operator behave differently for reading and writing. I basically wanted to make a vector that can be queried about whether it is modified recently...
23
by: centurian | last post by:
I have compiled following program with g++/gcc 3.3.1 and "MS Visual C++ 6" and it ran without any errors. Does this thing make any sense? Is it of some use or some problem with grammar/CFG of...
8
by: JDT | last post by:
Hi, The last statement at the end of this post is to delete a set member 7.7. I set a break point inside ltstr::operator(). I found out that STL binary-seach the set twice. How come? I have...
12
by: Alan Ning | last post by:
I have a question on assignment operator. People in my company likes to make their assignment operator const. So we have const A& operator=(const A& obSrc) But everywhere online, I see
42
by: Sheldon | last post by:
Hi, This program works when tested with gdb ( see results 1) but when used in a larger program where 12 becomes 1500 there exists a problem when freeing the memory ( see results 2). Can anyone...
7
by: MWimmer | last post by:
Dear members of this group, recently I came across a problem with repsect to operator=() and inheritance. Consider the following code snippet: ...
4
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Operator overloads are just like any other member function, you can make them do whatever you want. However, of course, we might expect them to behave in a certain way. The ++ operator should...
15
by: brad | last post by:
Do maps convert to multimaps if the same key is inserted more than once?
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.