C++ STL: Which method of iteration over a STL container is better?

后端 未结 9 1016
轻奢々
轻奢々 2020-12-09 11:43

This may seem frivolous to some of you, but which of the following 2 methods of iteration over a STL container is better? Why?



        
9条回答
  •  借酒劲吻你
    2020-12-09 12:19

    The first version works with any container and so is more useful in template functions that take any container a s a parameter. It is also conceivably slightly more efficient, even for vectors.

    The second version only works for vectors and other integer-indexed containers. It'd somewhat more idiomatic for those containers, will be easily understood by newcomers to C++, and is useful if you need to do something else with the index, which is not uncommon.

    As usual, there is no simple "this one is better" answer, I'm afraid.

提交回复
热议问题