Converting between C++ std::vector and C array without copying

前端 未结 5 1289
清酒与你
清酒与你 2020-12-02 08:34

I would like to be able to convert between std::vector and its underlying C array int* without explicitly copying the data.

Does std::vector provide access to the u

5条回答
  •  独厮守ぢ
    2020-12-02 09:07

    One way of protecting yourself against size changes is to reserve the maximal space (or larger) that you will need:

    std::vector v(4,100); //Maybe need 
    v.reserve(40);             //reallocate to block out space for 40 elements
    

    This will ensure that push_backs won't cause reallocation of the existing data.

提交回复
热议问题