C++ 向量:push_back 一个 int 数组 => 数组是否被复制?
C++ vector: push_back an array of int => Does array get copied?
我是 C++ 对象生命周期的新手,所以请多多包涵。
我有一个动态分配的整数数组向量:std::vector<int*>
This page 说 "The content of val is copied (or moved) to the new element."
据我了解,我推入数组的内容可能会被移动或复制,但它什么时候移动,什么时候复制?
如果值是原始类型,我怀疑它被复制了?例如,int、char 等?
否则被复制了?这是否意味着我的数组将是 "moved"?
===================
编辑 1:我想知道的是假设我将向量传递给函数。在这个函数中,我分配了一个整数数组并将其放入向量中。一旦函数 returns 并且我回到了调用者,我仍然可以安全地访问刚刚被推入向量的数组吗?
编辑 2:有人建议使用 vector<vector<int>>
,所以我的问题是,如果我将 "parent" 向量传递给某个函数。在此函数中,我创建了内部向量并将其推入外部向量。当我回到调用者那里时,我是否仍然可以安全地访问刚刚被推入外部向量的新内部向量?
像这样:
void foo()
{
vector<vector<int>> parentV;
addVect(parentV);
//Is is safe to access parentV[0][0] here?
}
void addVect(vector<vector<int>> &parentV)
{
vector<int> child;
child.push_back(1);
child.push_back(2);
parentV.push_back(child);
}
向量中存储的指针可能会移动,但它指向的地址不会移动,这意味着您的数据永远不会移动。如果您在向量中有实际的整数,然后获取其中一个的地址,那么如果必须重新分配向量,该指针可能会失效,但是由于只有指针存储在向量中,您的实际整数在永远不会重定位数组。
some suggested using vector>, so my question became, if I
pass the "parent" vector into some function. In this function, I
create the inner vector and push it into the outer vector. When I'm
back to the caller, can I still safely access the new inner vector
that was just pushed into the outer vector?
void foo()
{
vector<vector<int>> parentV;
addVect(parentV);
//Is the "child" vector still safe for access here?
}
void addVect(vector<vector<int>> &parentV)
{
vector<int> child;
child.push_back(1);
child.push_back(2);
parentV.push_back(child);
}
回答:
Is the "child" vector still safe for access here?
不,是。
不,至少不是通过名字 child
。 child
是 addVect
的局部变量,因此 foo
对此一无所知。 addVect
returns后销毁。
是的,您 可以 通过 parentV
访问它的值,因为它们已被复制到 parentV
而您正在传递 parentV
作为参考。
auto copyOfChild = *parentV.rbegin(); //get the last vector since push_back adds to the end of the vector
或
auto copyOfChild = parentV[parentV.size() - 1]; //get the last vector since push_back adds to the end of the vector
but when is it moved and when is it copied?
它基于您传递的值类型。例如 std::vvector::push_back()
有 2 overrides:
void push_back( const T& value );
void push_back( T&& value );
因此,如果您传递可以绑定到右值引用的类型,则会调用第二个方法并移动对象。否则调用第一个方法并复制对象。
I suspect the valued is copied if it's of primitive types? E.g., int, char, etc?
不,它是基于值类型,而不是 if type primitive 等等。
例如:
std::vector<Foobar> vec;
Foobar f;
vec.push_back( Foobar() ); // temporary binds to rvalue, move
vec.push_back( f ); // value copied
vec.push_back( std::move( f ) ); // now f moved
Does that means my array would be "moved"?
不可以,您的阵列无法移动。 Vector 只能移动传递给它的变量。您的变量是指针,而不是数组。对于原始指针移动与将一个指针复制到另一个指针相同。
我是 C++ 对象生命周期的新手,所以请多多包涵。
我有一个动态分配的整数数组向量:std::vector<int*>
This page 说 "The content of val is copied (or moved) to the new element."
据我了解,我推入数组的内容可能会被移动或复制,但它什么时候移动,什么时候复制?
如果值是原始类型,我怀疑它被复制了?例如,int、char 等?
否则被复制了?这是否意味着我的数组将是 "moved"?
===================
编辑 1:我想知道的是假设我将向量传递给函数。在这个函数中,我分配了一个整数数组并将其放入向量中。一旦函数 returns 并且我回到了调用者,我仍然可以安全地访问刚刚被推入向量的数组吗?
编辑 2:有人建议使用 vector<vector<int>>
,所以我的问题是,如果我将 "parent" 向量传递给某个函数。在此函数中,我创建了内部向量并将其推入外部向量。当我回到调用者那里时,我是否仍然可以安全地访问刚刚被推入外部向量的新内部向量?
像这样:
void foo()
{
vector<vector<int>> parentV;
addVect(parentV);
//Is is safe to access parentV[0][0] here?
}
void addVect(vector<vector<int>> &parentV)
{
vector<int> child;
child.push_back(1);
child.push_back(2);
parentV.push_back(child);
}
向量中存储的指针可能会移动,但它指向的地址不会移动,这意味着您的数据永远不会移动。如果您在向量中有实际的整数,然后获取其中一个的地址,那么如果必须重新分配向量,该指针可能会失效,但是由于只有指针存储在向量中,您的实际整数在永远不会重定位数组。
some suggested using vector>, so my question became, if I pass the "parent" vector into some function. In this function, I create the inner vector and push it into the outer vector. When I'm back to the caller, can I still safely access the new inner vector that was just pushed into the outer vector?
void foo() { vector<vector<int>> parentV; addVect(parentV); //Is the "child" vector still safe for access here? } void addVect(vector<vector<int>> &parentV) { vector<int> child; child.push_back(1); child.push_back(2); parentV.push_back(child); }
回答:
Is the "child" vector still safe for access here?
不,是。
不,至少不是通过名字 child
。 child
是 addVect
的局部变量,因此 foo
对此一无所知。 addVect
returns后销毁。
是的,您 可以 通过 parentV
访问它的值,因为它们已被复制到 parentV
而您正在传递 parentV
作为参考。
auto copyOfChild = *parentV.rbegin(); //get the last vector since push_back adds to the end of the vector
或
auto copyOfChild = parentV[parentV.size() - 1]; //get the last vector since push_back adds to the end of the vector
but when is it moved and when is it copied?
它基于您传递的值类型。例如 std::vvector::push_back()
有 2 overrides:
void push_back( const T& value );
void push_back( T&& value );
因此,如果您传递可以绑定到右值引用的类型,则会调用第二个方法并移动对象。否则调用第一个方法并复制对象。
I suspect the valued is copied if it's of primitive types? E.g., int, char, etc?
不,它是基于值类型,而不是 if type primitive 等等。 例如:
std::vector<Foobar> vec;
Foobar f;
vec.push_back( Foobar() ); // temporary binds to rvalue, move
vec.push_back( f ); // value copied
vec.push_back( std::move( f ) ); // now f moved
Does that means my array would be "moved"?
不可以,您的阵列无法移动。 Vector 只能移动传递给它的变量。您的变量是指针,而不是数组。对于原始指针移动与将一个指针复制到另一个指针相同。