C++ Vector at/[] operator speed

后端 未结 9 1685
借酒劲吻你
借酒劲吻你 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 16:57

    Operator[] might be faster than at, because it isn't required to do bounds checking.

    You can make curr a reference to do what you want.

    MyClass & curr = myvec.at(i);
    

    You might also do some benchmarking before getting worried. Modern processors can handle thousands of operations per second quite easily.

提交回复
热议问题