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;
}
}
}
Traversing STL List
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?
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 McCloudfor(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 ;).
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.
[edit]
to clarify, it makes a copy of the current value, increments, then returns the copy of the beginning value.
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) {
...
}
// 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).
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?!
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?!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement