Author |
Message |
boogerlad
|
Posted: Wed Dec 14, 2011 6:30 pm Post subject: Set iterator for loop decrementer position |
|
|
code: | std::set <int> faster;
std::set <int>::iterator itr;
std::set <int>::reverse_iterator rit;
faster.insert(15);
faster.insert(22);
faster.insert(9001);
faster.insert(158);
faster.insert(220);
faster.insert(90501);
faster.insert(1115);
faster.insert(232);
faster.insert(9001);
for (itr = faster.end(); itr != faster.begin(); --itr)
{
std::cout << *itr << std::endl;
}
for (rit = faster.rbegin(); rit != faster.rend(); ++rit)
{
std::cout << *rit << std::endl;
} |
Those two for loops should print exactly the same, but it doesn't. However, if I move the --itr like so:
code: | for (itr = faster.end(); itr != faster.begin();)
{
--itr;
std::cout << *itr << std::endl;
} |
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. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Tony
|
Posted: 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. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
boogerlad
|
Posted: Wed Dec 14, 2011 8:04 pm Post subject: RE:Set iterator for loop decrementer position |
|
|
Thanks so much! It all makes sense now! |
|
|
|
|
|
boogerlad
|
Posted: 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? |
|
|
|
|
|
Tony
|
Posted: Wed Dec 14, 2011 9:40 pm Post subject: RE:Set iterator for loop decrementer position |
|
|
which code example are you referring to? |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
boogerlad
|
Posted: Wed Dec 14, 2011 9:44 pm Post subject: Re: Set iterator for loop decrementer position |
|
|
code: | std::set <int> faster;
std::set <int>::iterator itr;
faster.insert(15);
faster.insert(22);
faster.insert(9001);
faster.insert(158);
faster.insert(220);
faster.insert(90501);
faster.insert(1115);
faster.insert(232);
faster.insert(9001);
for (itr = faster.end(); itr != faster.begin(); )
{
--itr;
std::cout << *itr << std::endl;
} |
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? |
|
|
|
|
|
Tony
|
|
|
|
|
boogerlad
|
Posted: 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! |
|
|
|
|
|
Sponsor Sponsor
|
|
|
|