
15 Jun
2004
15 Jun
'04
12:40 p.m.
Bo Peng wrote:
Bo Peng wrote:
I read /usr/include/c++/3.2.2/bits/stl_vector.h, here is the answer to my own questions:
1. How is size() implemented in vector<>?
end - start pointer. Not a reference to a variable. Too bad.
You aren't exactly required to call size() every iteration, you know. for( int i = 0, n = v.size(); i < n; ++i ) will work fine (or 'size_t i ...' if you like unsigned). Otherwise the optimizer would need to prove that v.size() is invariant. The idiomatic approach is to use iterators, of course, but compilers didn't seem to vectorize iterator-based loops last time I tried.