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

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.

Author:  Tony [ Wed Dec 14, 2011 7:37 pm ]
Post subject:  RE:Set iterator for loop decrementer position

code:

for( A ; B ; C )
   D

is functionally equivalent to
code:

A
while(B) {
  D
  C
}

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

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?

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!


: