Computer Science Canada Set iterator for loop decrementer position |
Author: | boogerlad [ Wed Dec 14, 2011 6:30 pm ] | ||||
Post subject: | Set iterator for loop decrementer position | ||||
Those two for loops should print exactly the same, but it doesn't. However, if I move the --itr like so:
then it works out fine. Can anyone explain why this is so? I would've thought that having the decrementer in the for loop statement and the first line would make no difference in execution. Btw, I know you can use a reverse iterator, but I'm just curious as to why this happens. |
Author: | Tony [ Wed Dec 14, 2011 7:37 pm ] | ||||
Post subject: | RE:Set iterator for loop decrementer position | ||||
is functionally equivalent to
That should be enough for you to figure out why moving that last statement into the loop is different. |
Author: | boogerlad [ Wed Dec 14, 2011 8:04 pm ] |
Post subject: | RE:Set iterator for loop decrementer position |
Thanks so much! It all makes sense now! |
Author: | boogerlad [ Wed Dec 14, 2011 9:20 pm ] |
Post subject: | RE:Set iterator for loop decrementer position |
wait a minute, I understand the differences in the placement of the decrementer, but now I have another question. If the decrementer returns an object (in this case, an integer), then why isn't the first element skipped when printing it out? |
Author: | Tony [ Wed Dec 14, 2011 9:40 pm ] |
Post subject: | RE:Set iterator for loop decrementer position |
which code example are you referring to? |
Author: | boogerlad [ Wed Dec 14, 2011 9:44 pm ] | ||
Post subject: | Re: Set iterator for loop decrementer position | ||
This works perfectly, but wouldn't the first thing that get's printed not be the last item in the set because it's getting decremented and the iterator returns an integer? |
Author: | Tony [ Wed Dec 14, 2011 10:42 pm ] |
Post subject: | RE:Set iterator for loop decrementer position |
you are wrong on two accounts 1) iterator is not an integer, it's of type iterator 2) #end http://www.cplusplus.com/reference/stl/set/end/ Quote: Returns an iterator referring to the past-the-end element in the set container. |
Author: | boogerlad [ Wed Dec 14, 2011 10:48 pm ] |
Post subject: | RE:Set iterator for loop decrementer position |
hmmm, thanks. I should probably read the stl api a bit more thoroughly before I make my self look like an idiot, thanks! |