reverse iterators and erasing

C

Christopher

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;
 
C

Christopher

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"
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top