Performance of pIter != cont.end() in for loop

后端 未结 7 441
面向向阳花
面向向阳花 2021-01-07 18:11

I was getting through \"Exceptional C++\" by Herb Sutter lately, and I have serious doubts about a particular recommendation he gives in Item 6 - Temporary Objects.

7条回答
  •  臣服心动
    2021-01-07 18:35

    Use std algorithms

    He's right of course; calling end can instantiate and destroy a temporary object, which is generally bad.

    Of course, the compiler can optimise this away in a lot of cases.

    There is a better and more robust solution: encapsulate your loops.

    The example you gave is in fact std::find, give or take the return value. Many other loops also have std algorithms, or at least something similar enough that you can adapt - my utility library has a transform_if implementation, for example.

    So, hide loops in a function and take a const& to end. Same fix as your example, but much much cleaner.

提交回复
热议问题