Examples showing how to iterate over a std::map
are often like that:
MapType::const_iterator end = data.end();
for (MapType::const_iterator it = data.begin(); it != end; ++it)
i.e. it uses ++it
instead of it++
. Is there any reason why? Could there be any problem if I use it++
instead?
it++
returns a copy of the previous iterator. Since this iterator is not used, this is wasteful. ++it
returns a reference to the incremented iterator, avoiding the copy.
Please see Question 13.15 for a fuller explanation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With