STL map weird behavior

Discussion in 'C++' started by Clint Weisbrod, Nov 18, 2007.

  1. 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.
     
    Clint Weisbrod, Nov 18, 2007
    #1
    1. Advertising

  2. "Clint Weisbrod" <> wrote in message
    news:...

    > 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?
     
    Andrew Koenig, Nov 18, 2007
    #2
    1. Advertising

  3. > 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.
     
    Clint Weisbrod, Nov 18, 2007
    #3
  4. Clint Weisbrod

    Ron Natalie Guest

    Clint Weisbrod wrote:
    >> 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.


    Check the copy constructors and assignment operators of your classes.
     
    Ron Natalie, Nov 19, 2007
    #4
  5. Clint Weisbrod

    Jim Langston Guest

    "Clint Weisbrod" <> wrote in message
    news:...
    >> 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.


    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.

    > 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.
     
    Jim Langston, Nov 19, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marcus
    Replies:
    2
    Views:
    598
    Marcus
    Dec 9, 2005
  2. Replies:
    2
    Views:
    562
    klaus hoffmann
    Feb 22, 2006
  3. kl
    Replies:
    7
    Views:
    1,307
    James Kanze
    Jan 1, 2008
  4. dorayme
    Replies:
    1
    Views:
    630
    richard
    Jan 21, 2011
  5. Luca Risolia

    STL map to STL vector

    Luca Risolia, Jan 13, 2014, in forum: C++
    Replies:
    32
    Views:
    397
    Seungbeom Kim
    Jan 18, 2014
Loading...

Share This Page