C++ vector push_back

前端 未结 7 928
旧时难觅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:29

    Yes pushing newRadio will push a copy of the Radio object into the vector. You also don't need to free the vector because it is local only so it will be destroyed once you are out of its scope. If for instance you wrote

    vector *m_radios = new vector();
    

    Then you would have to free that memory by calling the vector destructor manually.

提交回复
热议问题