c++ - Clarification on postfix/prefix operators on iterators -
in accelerated c++
andrew koenig
writes following code introduction templates , generic functions. code skip first element in container? or copy first iterator object before incrementing:
template<class in, class out> out copy(in begin, in end, out dest) { while (begin != end) *dest++ = *begin++; return dest; }
in other words, behave code?:
template<class in, class out> out copy(in begin, in end, out dest) { while (begin != end) *dest = *begin; dest++; begin++; return dest; }
post incrementing iterator (or else matter) may confusing doing pretty straight forward. makes copy of value, increments original , returns copy. location being referred "*dest++" same "*dest", difference after statement finishes dest refer next iteration in iteratable.
the problems arise when programmers use incremented variable repeatedly in same expression. (which used brain teaser , resulting effect varies language language)
Comments
Post a Comment