STL map weird behavior

C

Clint Weisbrod

All,

I'm creating an STL map object like this:

map<double, string> myMap;

I add a few items to the map using the [] operator.

If I iterate over the map like:

map<double, string>::iterator it, itLast;
for (it = myMap.begin(); it != myMap.end(); ++it)
{
itLast = it;
}

Then itLast should reference the last item in the map, (the item with
largest key). This works fine. My problem is if I do the following:

map<double, string>::iterator itLast2 = myMap.end();
--itLast2;

Then itLast2 does not reference the same item in the map. In fact, it
always ends up referencing the first item (with smallest key)!

Now I thought that using a double type key would be a problem, but
after researching this a bit, using double types (although probably
not a good idea) should work the way I expect.

Has anyone come across a similar problem?

Thanks everyone.

Clint Weisbrod.
 
A

Andrew Koenig

If I iterate over the map like:

map<double, string>::iterator it, itLast;
for (it = myMap.begin(); it != myMap.end(); ++it)
{
itLast = it;
}

Then itLast should reference the last item in the map, (the item with
largest key).

Unless the map is empty, in which case the effect is undefined.
My problem is if I do the following:

map<double, string>::iterator itLast2 = myMap.end();
--itLast2;

Then itLast2 does not reference the same item in the map

Again, if the map is empty, the effect is undefined.
In fact, it
always ends up referencing the first item (with smallest key)!

I'm skeptical. How about showing us a complete program that misbehaves as
you describe?
 
C

Clint Weisbrod

I'm skeptical. How about showing us a complete program that misbehaves as
you describe?

I'd be skeptical, too. Unfortunately, the code where this is happening
is quite large. Of course, I've written a small stand-alone program
and the STL behaves the way I expect. So one would think my large
application is at fault somehow. Just not able to see anything
obviously wrong.

BTW, this is happening on Mac OS X Leopard using Xcode 3.0. Wish I had
a 10.4 (Tiger) machine to try. Microsoft's VC++ 2005 compiler gives me
the results I expect.

Thanks for the response.

Clint.
 
R

Ron Natalie

Clint said:
I'd be skeptical, too. Unfortunately, the code where this is happening
is quite large. Of course, I've written a small stand-alone program
and the STL behaves the way I expect. So one would think my large
application is at fault somehow. Just not able to see anything
obviously wrong.

Check the copy constructors and assignment operators of your classes.
 
J

Jim Langston

Clint Weisbrod said:
I'd be skeptical, too. Unfortunately, the code where this is happening
is quite large. Of course, I've written a small stand-alone program
and the STL behaves the way I expect. So one would think my large
application is at fault somehow. Just not able to see anything
obviously wrong.

At the place you are observing this strange value for end()--, try also
looking at begin() and see if they are the same value. I would suspect that
if you are observing this behavior then begin() will be showing the largest
value. Perhaps your map's sort index is wrong or something. You might also
want to try to dump the value of the keys at the point this is happening and
see if you find anything weird.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top