问题描述
将新对象元素推送到 std :: vector
上的正确方法是什么?我想要的数据在向量中分配。这会将对象 newradio
复制到向量中,然后当它超出范围时除去 newradio
)
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 newradio
into the vector and then get rid of newradio
when it goes out of scope (e.g. out of the stack)?
vector<Radio> m_radios;
Radio newradio(radioNum);
m_radios.push_back(newradio);
然后当我释放包含 m_radios
,这将释放由向量分配的所有内存?
And then when I free the object containing m_radios
, will this free all the memory allocated by the vector?
推荐答案
std :: vector
管理自己的内存。这意味着,当调用向量的析构函数时,向量中保存的内存被释放。
std :: vector
也会在删除时调用对象的析构函数(通过 erase
, pop_back
,清除
或向量的析构函数)。
std::vector
manages its own memory. That means that, when the destructor of a vector is invoked the memory held by the vector is released. std::vector
also invokes an object's destructor when it is removed (through erase
, pop_back
, clear
or the vector's destructor).
p>
When you do this:
Radio newradio(radioNum);
m_radios.push_back(newradio);
您添加了 newradio
使用 Radio
的副本构造函数)。 newradio
将在范围超出范围时被销毁,并且当从向量中删除时(如任何对象),副本将被销毁。
You add a copy of newradio
(created using Radio
's copy constructor) to the vector. newradio
will be destroyed when it goes out of scope, and the copy will be destroyed when it is removed from the vector (as for any object).
这是一个重要的点: std :: vector
只存储对象的副本,这意味着对象必须有一个有意义的复制构造函数(和赋值运算符,但这是另一个问题)。如果你有一个指针的向量,那么指针本身将被复制,而不是它指向的东西。请注意,对于每个标准容器(例如 std :: list
或 std :: set
),此行为是相同的。
That's an important point: std::vector
only stores copies of an object, which means the object must have a meaningful copy constructor (and an assignment operator, but that's another issue). If you have a vector of pointers, then the pointer itself will be copied, and not what it points to. Note that this behavior is the same for every standard container (like std::list
or std::set
).
作为一个经验法则,如果你不使用指针,那么你不必担心自己释放内存。
As a rule of thumb, if you're not using pointers, then you don't have to worry about releasing the memory yourself.
在C ++ 11中,大多数标准容器(包括向量
)都有 emplace_back
方法,该方法在容器的末尾构造对象 。它需要一组参数,并调用与这些参数最匹配的构造函数(如果没有这样的构造函数,则调用失败),使用所述构造函数在容器末尾创建没有任何副本的对象。所以,上面的代码可以改写为:
In C++11, most standard containers (including vector
) have an emplace_back
method that constructs an object in place at the end of the container. It takes a bunch of parameters and calls the constructor that best matches those parameters (or fails if no such constructor exist), using said constructor to create the object, without any copy, at the end of the container. So, the above code could be rewritten as:
m_radios.emplace_back(radioNum); // construct a Radio in place,
// passing radioNum as the constructor argument
在C ++ 11中,容器通常移动感知,因此它们不再需要对象是可复制的:如果它们是可移动的,则容器将根据需要移动其内容(例如在重新分配期间)。如果您要复制向量,仍需要可复制类型。
Also in C++11, containers are usually move aware, so they no longer require objects to be copiable: if they are movable, then the container will move its contents as required (such as during reallocations, for instance). Copiable types are still required if you want to copy the vector.
这篇关于C ++向量push_back的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!