[Cs3] Operator Precedence

Mikhail Nesterenko mikhail at cs.kent.edu
Mon Feb 6 13:44:54 EST 2023


> index that contains 7 and then dereferences it, causing the 7 to print. The
> fourth, *(it + 5), seems logical enough as it increases the index by 5 and
> then dereferences it, causing a 2 to be printed. The final one, dOne[it -
> dOne.begin() + 2], I do not understand how 5 is printed. 2 came from index
> 12, dOne.begin() returns in iterator pointing at the first element, so
> index 0, then there is a +2.


regarding this example:  http://antares.cs.kent.edu/~mikhail/classes/cs3/Examples/STLContainersSequential/Vectors/deque.cpp

*(it+5)

dos not change the value of the iterator it  !

by the time of the below expression is evaluated, iterator it is
pointing to the 7th position.


dOne[it - dOne.begin() + 2]

The above code is equivalent to dOne[9]

More fun with iterators and repsedence is here:

https://stackoverflow.com/questions/859770/post-increment-on-a-dereferenced-pointer

thanks,
-- 
Mikhail


More information about the cs3 mailing list