Jump to content
  • Advertisement
Sign in to follow this  
Broni

Traversing STL List

This topic is 4606 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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 ;).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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) {
...
}

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

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)
{
}


Share this post


Link to post
Share on other sites
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?!

Share this post


Link to post
Share on other sites
Thanks for your responses, It enabled me to iterate more elegantly. Now my key frame editor, is playing back animation sequences!!

Thanks,
Edwin

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!