C++ vector push_back

前端 未结 7 936
旧时难觅i
旧时难觅i 2020-12-23 22:03

What is the proper way of pushing a new object element onto a std::vector? I want the data to be allocated in the vector. Will this copy the object newrad

7条回答
  •  鱼传尺愫
    2020-12-23 22:17

    By default, std::vector manages the memory itself using the copy constructor of the underlying class. So, it acts a bit as if the vector element was a local variable (when the vector goes out of scope, the element gets destructed).

    When you don't want this behavior, you can use a vector of pointer or boost::ptr_vector instead.

提交回复
热议问题