reverse iterators and erasing

Discussion in 'C++' started by Christopher, Feb 25, 2009.

  1. Christopher

    Christopher Guest

    I am using reverse iterators for the first time. I think there is
    something wrong with the way I use it to erase elements in my source
    vector. My result here is not what I expect. Can anyone spot what I am
    doing wrong?

    What I am trying to do.
    Given a vector of objects that describe a display mode, map them by
    resolution (resolution being an attribute of a display mode)

    So, I get 66 in with the first 3 being 600X480.
    when I run my code, my result contains a map containing one vector of
    3 elements.
    It seems I keep grabbing the same 3 from the front, when I expect them
    to have been erased after the first iteration.

    Code:
    //------------------------------------------------------------------------------------------
    void DisplayModeEnumerator::MapCapsByResolution(DisplayModeCaps &
    displayModeCaps,

    DisplayModeCapsByResolution & displayModeCapsByResolution) const
    {
    // Start with an empty map
    displayModeCapsByResolution.clear();

    // Sort the display modes by resolution
    std::sort(displayModeCaps.begin(), displayModeCaps.end(),
    SortByResolution());

    // Seperate the display modes by resolution
    while( !displayModeCaps.empty() )
    {
    Resolution resolution = displayModeCaps.front().m_resolution;
    DisplayModeCaps::iterator start;
    DisplayModeCaps::reverse_iterator rend;

    start = std::find_if(displayModeCaps.begin(), displayModeCaps.end
    (), HasResolution(resolution));
    rend = std::find_if(displayModeCaps.rbegin(),
    displayModeCaps.rend(), HasResolution(resolution));

    DisplayModeCaps temp(start, rend.base());
    displayModeCapsByResolution[resolution] = temp;

    displayModeCaps.erase(start, rend.base());
    }

    // DEBUG
    unsigned size = 0;
    for(DisplayModeCapsByResolution::iterator it =
    displayModeCapsByResolution.begin(); it !=
    displayModeCapsByResolution.end(); ++it)
    {
    Resolution resolution = it->first;
    size += it->second.size();
    }
    }

    ------------------------------------
    types

    // Display Mode Descriptions
    typedef std::vector<DisplayModeCapability> DisplayModeCaps;

    // Key - Resolution
    // Value - Display mode capabilities
    typedef std::map<Resolution, DisplayModeCaps>
    DisplayModeCapsByResolution;
    Christopher, Feb 25, 2009
    #1
    1. Advertising

  2. Christopher

    Christopher Guest

    On Feb 25, 3:22 pm, Christopher <> wrote:
    > I am using reverse iterators for the first time. I think there is
    > something wrong with the way I use it to erase elements in my source
    > vector. My result here is not what I expect. Can anyone spot what I am
    > doing wrong?



    Solved my problem. My assignment operator wasn't working. Why it
    doesn't work is another post entitled "Derived class assignment"
    Christopher, Feb 26, 2009
    #2
    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. dogbite
    Replies:
    4
    Views:
    690
    osmium
    Oct 10, 2003
  2. Alexander Stippler

    reverse iterators revisited

    Alexander Stippler, Jan 1, 2004, in forum: C++
    Replies:
    0
    Views:
    367
    Alexander Stippler
    Jan 1, 2004
  3. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Replies:
    1
    Views:
    484
    Kai-Uwe Bux
    May 8, 2005
  4. awm129
    Replies:
    8
    Views:
    425
    Richard Herring
    Jul 27, 2009
  5. , India
    Replies:
    10
    Views:
    1,071
    James Kanze
    Aug 8, 2009
Loading...

Share This Page