In my game engine project, I make extensive use of the STL, mostly of the std::string
and std::vector
classes.
In many cases, I have to ite
You're doing it OK for vectors, although that doesn't translate into the right way for other containers.
The more general way is
for(std::vector::const_iterator i = theContainer.begin(); i != theContainer.end; ++i)
which is more typing than I really like, but will become a lot more reasonable with the redefinition of auto
in the forthcoming Standard. This will work on all standard containers. Note that you refer to the individual foo
as *i
, and use &*i
if you want its address.
In your loop, .size()
is executed every time. However, it's constant time (Standard, 23.1/5) for all standard containers, so it won't slow you down much if at all. Addition: the Standard says "should" have constant complexity, so a particularly bad implementation could make it not constant. If you're using such a bad implementation, you've got other performance issues to worry about.