简体   繁体   English

C ++向量:push_back int数组=>是否复制数组?

[英]C++ vector: push_back an array of int => Does array get copied?

I'm new to C++'s object lifetime, so bear with me. 我是C ++对象生命周期的新手,所以请多多包涵。

I have a vector of dynamically allocated arrays of integers: std::vector<int*> 我有一个动态分配的整数数组std::vector<int*>std::vector<int*>

This page says "The content of val is copied (or moved) to the new element." 该页面显示 “ val的内容已复制(或移动到)新元素。”

What I understand from this is that what I push into the array MAY be moved or copied, but when is it moved and when is it copied? 我从中了解到,我推入数组的内容可以移动或复制,但是何时移动以及何时复制?

I suspect the valued is copied if it's of primitive types? 我怀疑值是原始类型是否已复制? Eg, int, char, etc? 例如int,char等?

And it's copied otherwise? 并以其他方式复制吗? Does that means my array would be "moved"? 这是否意味着我的数组将被“移动”?

=================== ===================

EDIT 1 : what I'm trying to find out is that suppose I pass the vector into a function. 编辑1 :我想找出的是假设我将向量传递给函数。 In this function I allocate an array of integers and push it into the vector. 在此函数中,我分配一个整数数组并将其推入向量。 Once the function returns and I'm back to the caller, can I still safely access the array that was just pushed into the vector? 函数返回后,我又返回到调用方,我是否仍然可以安全地访问刚刚推送到向量中的数组?

EDIT 2 : some suggested using vector<vector<int>> , so my question became, if I pass the "parent" vector into some function. 编辑2 :有人建议使用vector<vector<int>> ,因此,如果我将“父”向量传递给某些函数,我的问题就变成了。 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? 当我返回到调用方时,是否仍可以安全地访问刚刚推送到外部向量中的新内部向量? Something like this: 像这样:

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);
}

The pointer stored in the vector may be moved, but the address it points to won't, which means your data never move. 向量中存储的指针可能会移动,但是它指向的地址不会移动,这意味着您的数据永远不会移动。 If you had the actual ints in the vector and then took an address to one of them, that pointer could be invalidated if the vector had to be re-allocated, but since only the pointer is stored in the vector, your actual ints in the arrays will never be relocated. 如果您在向量中具有实际的整数,然后使用了其中一个地址,那么如果必须重新分配该向量,则该指针可能会失效,但是由于仅将指针存储在向量中,因此,阵列将永远不会被重定位。

some suggested using vector>, so my question became, if I pass the "parent" vector into some function. 一些建议使用vector>,因此,如果我将“父”向量传递给某些函数,我的问题就变成了。 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); } 

To answer: 回答:

Is the "child" vector still safe for access here? 这里的“子”向量仍然安全吗?

No and yes. 不,是的。

No, not through the name child at least. 不,至少没有通过child的名字。 child is local to addVect and thus foo won't know anything about it. childaddVect本地addVect ,因此foo对此一无所知。 It will be destroyed after addVect returns. addVect返回后将销毁它。

Yes, you can access it's values through parentV since they've been copied to parentV and you're passing parentV as reference. 是的,您可以通过parentV访问它的值,因为它们已被复制到parentV并且您正在传递parentV作为参考。

auto copyOfChild = *parentV.rbegin(); //get the last vector since push_back adds to the end of the vector

or 要么

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? 但是什么时候移动,什么时候复制呢?

It is based on value type that you pass. 它基于您传递的值类型。 For example std::vvector::push_back() has 2 overrides: 例如std::vvector::push_back()有2个覆盖:

void push_back( const T& value );   
void push_back( T&& value );

so if you pass a type that can bind to rvalue refence, then second method is called and object is moved. 因此,如果传递的类型可以绑定到右值引用,则将调用第二个方法并移动对象。 Otherwise first method is called and object is copied. 否则,将调用第一个方法并复制对象。

I suspect the valued is copied if it's of primitive types? 我怀疑值是原始类型是否已复制? Eg, int, char, etc? 例如int,char等?

No it is based on value type, not if type primitive etc. For example: 不,它基于值类型,而不是基于基本类型等。例如:

 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"? 这是否意味着我的数组将被“移动”?

No your array cannot be moved. 没有您的阵列不能移动。 Vector could only move a variable that passed to it. 向量只能移动传递给它的变量。 Your variable is a pointer, not array. 您的变量是一个指针,而不是数组。 For raw pointer move is the same as copying one pointer to another. 对于原始指针,移动与将一个指针复制到另一指针相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM