Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > reverse iterators and erasing

Reply
Thread Tools

reverse iterators and erasing

 
 
Christopher
Guest
Posts: n/a
 
      02-25-2009
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;
 
Reply With Quote
 
 
 
 
Christopher
Guest
Posts: n/a
 
      02-26-2009
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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
plain iterators and reverse iterators on vector subramanian100in@yahoo.com, India C++ 10 08-08-2009 08:28 AM
erasing from vectors with iterators, what is going on here? awm129 C++ 8 07-27-2009 10:09 AM
Iterators and reverse iterators Marcin Kaliciński C++ 1 05-08-2005 09:58 AM
reverse iterators revisited Alexander Stippler C++ 0 01-01-2004 02:14 PM
Stacks Queues Reverse Reverse Polish dogbite C++ 4 10-10-2003 05:06 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57