S
Steven T. Hatton
Suppose I have the following:
vector<T> v(20);
for(vector<T>::it = v.begin(); it != v.end(); ++it){/*do something*/}
What can I reasonably expect of a compiler regarding the call to v.end()?
IOW, does it cost more to evaluate v.end() for each iteration than it does
to use
size_t sz = v.size();
for(size_t i = 0; i < sz; i++){/*do something*/}
?
I'm talking about reasonable expectations not what a specific compiler does.
I have to wonder what the advantage of the first form above is over the
second. I know there are functions in <algorithm> which require iterators,
but for purposes of traversing a sequence in a for() loop, it seems
generally more convenient to use the 'traditional' array-style with []
subscripting. That, however, is not what I see in a lot of C++ literature.
vector<T> v(20);
for(vector<T>::it = v.begin(); it != v.end(); ++it){/*do something*/}
What can I reasonably expect of a compiler regarding the call to v.end()?
IOW, does it cost more to evaluate v.end() for each iteration than it does
to use
size_t sz = v.size();
for(size_t i = 0; i < sz; i++){/*do something*/}
?
I'm talking about reasonable expectations not what a specific compiler does.
I have to wonder what the advantage of the first form above is over the
second. I know there are functions in <algorithm> which require iterators,
but for purposes of traversing a sequence in a for() loop, it seems
generally more convenient to use the 'traditional' array-style with []
subscripting. That, however, is not what I see in a lot of C++ literature.