On https://doc-snapshots.qt.io/qtcreator-extending/coding-style.html it is recommended to write for loops like the following:
Container::iterator end = large.end();
for (Container::iterator it = large.begin(); it != end; ++it) {
//...;
}
instead of
for (Container::iterator it = large.begin(); it != large.end(); ++it) {
//...;
}
Since I have rarely seen this style in any code, I would like to know whether the consecutive call of end() really adds a noticeable run-time overhead for large loops over stl containers or whether compilers already optimize such cases.
Edit: As many of to very good comments pointed out: This question is only valid if the code inside the loop does not modify the end iterator. Otherwise of course the repeated call of end is mandatory.
See Question&Answers more detail:os