C++ Vector at/[] operator speed

后端 未结 9 1690
借酒劲吻你
借酒劲吻你 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:05

    If it is the case, that you load up a vector, then process it without adding or deleting any more elements, then consider getting a pointer to the underlying array, and using array operations on that to 'avoid the vector overhead'.

    If you are adding or deleting elements as part of your processing, then this is not safe to do, as the underlying array may be moved at any point by the vector itself.

提交回复
热议问题