Can end() be a costly operation for stl containers

后端 未结 7 1959
囚心锁ツ
囚心锁ツ 2020-12-16 12:16

On https://doc-snapshots.qt.io/qtcreator-extending/coding-style.html it is recommended to write for loops like the following:

Container::iterator end = large         


        
7条回答
  •  轮回少年
    2020-12-16 12:47

    This is less about end being costly and more about the ability of a compiler to see that end will not change through a side effect in the loop body (that it is a loop invariant).

    The complexity of end is required to be constant by the standard. See table 96 in N3337 in 23.2.1.

    Using standard library algorithms circumvents the whole dilemma nicely.

提交回复
热议问题