For anyone reading this now, the question has become somewhat of a moot point with C++11.
I wasn't sure whether this response qualifies as an answer, because it doesn't actually address the point of the question. But I do think it's valid to point out that the problem raised here will be seldom encountered in practice for a C++11 programmer, and I certainly would have found this response useful a few years ago. This response is therefore aimed at the reader who simply wants to know the best way of iterating through all elements in a STL container (vector
, list
, deque
, etc.).
Assuming that the OP wanted access to each element in the container, we can easily sidestep the entire question of whether defining end
is sufficiently faster than calling Container::end()
by writing a range-based for loop:
Container container; // my STL container that has been filled with stuff
// (note that you can replace Container::value_type with the value in the container)
// the standard way
for (Container::value_type element : container) {
// access each element by 'element' rather than by '*it'
}
// or, if Container::value_type is large
Container container; // fill it with something
for (Container::value_type& element : container) {
//
}
// if you're lazy
Container container; // fill it with something
for (auto element : container) {
//
}
The OP has asked whether the trade-off between brevity of simply comparing it
to Container::end()
at every iteration and the performance of declaring a variable end
and comparing that at each step instead is worth it. Since range-based for loops provide a simple, easy to write and easy to read alternative that also happens to, internally, declare an end
iterator rather than calling the Container::end()
method at every step, the number of cases where we need to dwell on this question has been reduced to a limited number of cases.
As per cppreference.com, the range-based for loop will produce code with the same side-effects as the following:
{
auto && __range = range_expression ;
for (auto __begin = begin_expr,
__end = end_expr;
__begin != __end; ++__begin) {
range_declaration = *__begin;
loop_statement
}
}