Hi,

If I have a std::vector of objects and I need to iterate through all its elements, is it faster to do:

for (vector::iterator i = myvec.begin(); i != myvec.end(); ++i)
    //blah

or

for (int i = 0; i < myvec.size(); ++i)
   // myvec[i] blah

Or are they equally fast?

Thanks
Xorlium

As far as it likely matters to you, they're equally fast.

The second one would probably use less memory. And normally you would use the second one unless you have a compelling reason to use the first.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.