C++ Vector at/[] operator speed

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

    The reason the first doesn't work is that you're not setting a pointer or iterator to the address of the ith variable. Instead you're setting curr equal to the value of the ith variable and then modifying curr. I'm assuming that doThis and doThat are references.

    Do this:

    MyObject& curr = myvec.at( i );
    

提交回复
热议问题