Traversing STL List

Started by
8 comments, last by Broni 18 years ago
I am currently using STL List to store some animation data, but I wish to only access data up until the 2nd to last entry. How exactly can this be achieved using iterators and list?

globally defined 
typedef std::list<MAV_KFEKeyFrame*>::iterator KeyFrameIterator;

void Animation::computeAnimationSequence(std::list<MAV_KFEKeyFrame*> keyFrames)
{
 //do we have any keys to calculate
	if(keyFrames.size()>1)
	{
		for(KeyFrameIterator i=keyFrames.begin();i!=keyFrames.end();i++)
		{
			//is there two keyframes to compare
			KeyFrameIterator next = i;
			next++;

			int frameDiff = (*next)->frameNumber - (*i)->frameNumber;
	
			KeyFrameIterator whatsNext = next;
			if(++whatsNext==keyFrames.end())return;	
		}
			
		
	}
}

Advertisement
for(KeyFrameIterator i=keyFrames.begin();(i+1)!=keyFrames.end();i++){    // yada, yada}


edit - assuming you know there is at least 1 frame in the list
globally defined typedef std::list<MAV_KFEKeyFrame*>::iterator KeyFrameIterator;void Animation::computeAnimationSequence(std::list<MAV_KFEKeyFrame*> keyFrames){ //do we have any keys to calculate	if(keyFrames.size()>1)	{		for(KeyFrameIterator next=keyFrames.begin(), i = next++;next!=keyFrames.end();i++, next++)		{			int frameDiff = (*next)->frameNumber - (*i)->frameNumber;		}	}}

Haven't tested it, but I'm pretty sure it'll work.

CM
Quote:Original post by Conner McCloud
for(KeyFrameIterator next=keyFrames.begin(), i = next++;next!=keyFrames.end();i++, next++)

CM


Shouldn't that line be the following:
for(KeyFrameIterator next=keyFrames.begin(), i = next+1;next!=keyFrames.end();i++, next++)
Maybe its because I've been awake for going on 20 hours now, but isn't yours going to increment next intially skipping the first node? Just a thought, I'm starting to second guess myself now maybe its time to get some coffee before class ;).

-John "bKT" Bellone [homepage] [[email=j.bellone@flipsidesoftware.com]email[/email]]
post increment returns the current value then increments. So that will intialize i to be equal to begin() and next will be begin() + 1

[edit]
to clarify, it makes a copy of the current value, increments, then returns the copy of the beginning value.

moe.ron
You could always use reverse iterators:


// Since rbegin points to the last item we only need to decrease
// by one to get the next to last item

std::list::reverse_iterator iterEnd(keyFrames.rbegin());
++iterEnd;

// Since rend is actually before the first item we need to increase
// by one to get the first

std::list::reverse_iterator iter(keyFrames.rend());
--iter;

// Simple traversal

for (; iterEnd != iter; --iter) {
...
}
Are you sure you shouldn't pass the list by const reference instead of making a copy every time? (This would also require using const_iterator instead of iterator).
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
std::list<MAV_KFEKeyFrame*>::const_iterator iterStart(keyFrames.begin() + 1);std::list<MAV_KFEKeyFrame*>::const_iterator iterEnd(keyFrames.end() - 1);for(std::list<MAV_KFEKeyFrame*>::const_iterator i = iterStart; i != iterEnd; ++i){}

Ftn: The original poster didn't want to skip the first item too. Also iirc, you shouldn't decrement from the end() iterator.

Why not simply pop_back, then iterate, then push_back? It's nice and clean and easy to maintain that way.[cool]

Otherwise, using reverse iterators and skipping the first item, might be even better, provided you don't mind which order you iterate throught the list.

I take it you will use an assertion to check that the list is never empty, just prior to doing this?!
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Thanks for your responses, It enabled me to iterate more elegantly. Now my key frame editor, is playing back animation sequences!!

Thanks,
Edwin

This topic is closed to new replies.

Advertisement