Examples showing how to iterate over a std::map are often like that:
MapType::const_iterator end = data.end();
for (MapType::const_iterator it = data.begin(); it != end; ++it)
i.e. it uses ++it instead of it++. Is there any reason why? Could there be any problem if I use it++ instead?
Putting it to the test, I made three source files:
After compiling with
g++ -S -O3, GCC 4.6.1, I find that version 2 and 3 produce identical assembly, and version 1 differs only in one instruction,cmpl %eax, %esivscmpl %esi, %eax.So, take your pick and use whatever suits your style. Prefix increment
++itis probably best because it expresses your requirements most accurately, but don’t get hung up about it.