473,473 Members | 2,215 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

unexpected map behaviour

Hi all,

I'm using a std::map with an own class as key-type. afaik, it should be
sufficient to define a strict ordering, using an overloaded operator< to
allow for the map to function. However, when i iterate over the map after
adding a few items, it seems that the same key does get added twice.

It would be great if somebody could point out what (probably obvious)
mistake i'm making in the code below:

//-----begin listing-------->
class triplet {
public:
int first,sec,third;

triplet(): first(-1),sec(-1),third(-1){}
triplet(int a, int b, int c): first(a),sec(b),third(c){}

triplet(const triplet& t): first(t.first),sec(t.sec),third(t.third){}

bool operator<(const triplet& o) const {
return (first<o.first?true:(sec<o.sec?true:third<o.third) );
}

friend ostream& operator<<(ostream& os, const triplet& t) {
os<<"["<<t.first<<","<<t.sec<<","<<t.third<<"]";
return os;
}
};

int main() {
map <triplet, list<int> > m;

for (int i=0; i<3; i++) {
m[triplet(i,i+10,i+20)].push_back(1);
m[triplet(i,i+10,i+30)].push_back(1);
}

for (int i=0; i<3; i++) {
cout<<triplet(i,i+10,i+20)<<":"<<m[triplet(i,i+10,i+20)].size()<<"\n";
cout<<triplet(i,i+10,i+30)<<":"<<m[triplet(i,i+10,i+30)].size()<<"\n";
}
for (map <triplet, list<int> >::iterator m_i=m.begin(); m_i!=m.end();
m_i++)
cout<<m_i->first<<" -> "<<(m_i->second).size()<<endl;
}
//<-----end listing--------

the output is:
[0,10,20]:1
[0,10,30]:0
[1,11,21]:1
[1,11,31]:1
[2,12,22]:1
[2,12,32]:1
====================================
[0,10,20] -> 1
[1,11,21] -> 1
[0,10,30] -> 1
[2,12,22] -> 1
[0,10,30] -> 0
[1,11,31] -> 1
[2,12,32] -> 1

so key [0,10,30] was added twice.

Many thanks,
Steven
--
Steven Van den Berghe
steven.vandenberghe <at> intec <dot> ugent <dot> be
Workgroup Broadband Communication Networks
Department Information Technology
Ghent University - Belgium

Jul 19 '05 #1
2 1750
Steven Van den Berghe wrote:
Hi all,

I'm using a std::map with an own class as key-type. afaik, it should
be sufficient to define a strict ordering, using an overloaded
operator< to allow for the map to function. However, when i iterate
over the map after adding a few items, it seems that the same key does
get added twice.
Your comparison function is not correct.
It would be great if somebody could point out what (probably obvious)
mistake i'm making in the code below:

//-----begin listing-------->
class triplet {
public:
int first,sec,third;

triplet(): first(-1),sec(-1),third(-1){}
triplet(int a, int b, int c): first(a),sec(b),third(c){}

triplet(const triplet& t): first(t.first),sec(t.sec),third(t.third){}

bool operator<(const triplet& o) const {
return (first<o.first?true:(sec<o.sec?true:third<o.third) );
}


Let's get an example:

[2 3 4] < [3 2 1] is true

[3 2 1] < [2 3 4] is also true

This is not allowed in a map.

Jul 19 '05 #2

"Steven Van den Berghe" <st*****************@ugent.be> schrieb im Newsbeitrag news:bi**********@gaudi2.UGent.be...
[snip]
bool operator<(const triplet& o) const {
return (first<o.first?true:(sec<o.sec?true:third<o.third) );
}

[/snip]

should read:
bool operator < const triplet& o) const
{
if (first < o.first)
return true;
if (o.first < first)
return false;
if (second < o.second)
return true;
if (o.second < second)
return false;
return (third < o.third);
}

Christian
Jul 19 '05 #3

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

Similar topics

2
by: Gerhard Esterhuizen | last post by:
Hi, I am observing unexpected behaviour, in the form of a corrupted class member access, from a simple C++ program that accesses an attribute declared in a virtual base class via a chain of...
10
by: sindica | last post by:
I am using DevC++ 4.0 lately, which uses Mingw port of GCC, on a WinXP. I am surprised to see the malloc behaviour which is not consistent with the documentation. See the program and its output...
9
by: Jeff Louie | last post by:
In C# (and C++/cli) the destructor will be called even if an exception is thrown in the constructor. IMHO, this is unexpected behavior that can lead to an invalid system state. So beware! ...
8
by: Steven D'Aprano | last post by:
I came across this unexpected behaviour of getattr for new style classes. Example: >>> class Parrot(object): .... thing = .... >>> getattr(Parrot, "thing") is Parrot.thing True >>>...
2
by: bb | last post by:
Hi, I am using gcc v4.0.2 on fedora core 4 (2.6.16). Any reason why the handler set thru' set_unexpected() never gets called in the following code? --------- Code ------------- #include...
4
by: conan | last post by:
This regexp '<widget class=".*" id=".*">' works well with 'grep' for matching lines of the kind <widget class="GtkWindow" id="window1"> on a XML .glade file However that's not true for the...
7
by: Andrew McLean | last post by:
I have a bunch of csv files that have the following characteristics: - field delimiter is a comma - all fields quoted with double quotes - lines terminated by a *space* followed by a newline ...
23
by: gu | last post by:
hi to all! after two days debugging my code, i've come to the point that the problem was caused by an unexpected behaviour of python. or by lack of some information about the program, of course!...
1
by: Just Another Victim of the Ambient Morality | last post by:
HTMLParser is behaving in, what I find to be, strange ways and I would like to better understand what it is doing and why. First, it doesn't appear to translate HTML escape characters. I don't...
5
by: sukkopera | last post by:
Hi, I have just encountered a Python behaviour I wouldn't expect. Take the following code: ------------------------------------------------------------------------ class Parent: a = 1 def m...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.