Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   reverse iterators and erasing (http://www.velocityreviews.com/forums/t672412-reverse-iterators-and-erasing.html)

Christopher 02-25-2009 09:22 PM

reverse iterators and erasing
 
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(Display ModeCaps &
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 02-26-2009 04:19 AM

Re: reverse iterators and erasing
 
On Feb 25, 3:22*pm, Christopher <cp...@austin.rr.com> 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"


All times are GMT. The time now is 04:47 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.