C++ Vector at/[] operator speed

后端 未结 9 1532
借酒劲吻你
借酒劲吻你 2021-01-02 16:19

In order to give functions the option to modify the vector I can\'t do

curr = myvec.at( i );
doThis( curr );
doThat( curr );
doStuffWith( curr );
         


        
9条回答
  •  别那么骄傲
    2021-01-02 17:04

    The complexity of at() is constant, i.e., in practice this means that it must be designed to not have any relevant performance penalty.

    You can use [], which is also constant complexity, but does not check bounds. This would be equivalent to using pointer arithmetic and, thus, potentially a bit faster than the former.

    In any case, vector is specifically designed for constant performance access to any of its elements. So this should be the least of your worries.

提交回复
热议问题