Theres a slight performance advantage when using pre-increment statements and post-increment statements. When setting up loops that use iterators, you should use pre-increments:
for (list<string>::const_iterator it = tokens.begin(); it != tokens.end(); ++it) {
The reason becomes clear when you think about how both statements are usually executed. The preliminary increment is quite simple. However, in order for the post-increment to work, you first need to make a copy of the object, make the actual increment of the original object, and then return the copy:
class MyInteger { private: int m_nValue; public: MyInteger(int i) { m_nValue = i; }
As you can see, the implementation after the increment includes two additional copy operations. This can be quite expensive if the item in question is bulky. Having said that, some compilers may be smart enough to get away with a single copy operation through optimization. The fact is that a post-increment will usually include more work than a pre-increment, and therefore it is wise to use it to place your "++" in front of your iterators, and not after.
(1) Credit to a linked website.
user195488
source share