C++ 将一个向量复制到另一个向量的快速方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/644673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:27:23  来源:igfitidea点击:

fast way to copy one vector into another

c++algorithmstl

提问by Daniel Earwicker

I prefer two ways:

我更喜欢两种方式:

void copyVecFast(const vec<int>& original)
{
  vector<int> newVec;
  newVec.reserve(original.size());
  copy(original.begin(),original.end(),back_inserter(newVec));
}

void copyVecFast(vec<int>& original)
{

  vector<int> newVec;
  newVec.swap(original); 
}

How do you do it?

你怎么做呢?

采纳答案by rlbond

Your second example does not work if you send the argument by reference. Did you mean

如果您通过引用发送参数,则您的第二个示例不起作用。你的意思是

void copyVecFast(vec<int> original) // no reference
{

  vector<int> new_;
  new_.swap(original); 
}

That would work, but an easier way is

那会奏效,但更简单的方法是

vector<int> new_(original);

回答by Daniel Earwicker

They aren't the same though, are they? One is a copy, the other is a swap. Hence the function names.

不过它们不一样,是吗?一个是副本,另一个是交换。因此,函数名称。

My favourite is:

我最喜欢的是:

a = b;

Where aand bare vectors.

其中ab是向量。

回答by X-Istence

This is another valid way to make a copy of a vector, just use its constructor:

这是复制向量的另一种有效方法,只需使用其构造函数:

std::vector<int> newvector(oldvector);

This is even simpler than using std::copyto walk the entire vector from start to finish to std::back_insertthem into the new vector.

这比使用std::copy从头到尾将整个向量遍历std::back_insert到新向量中更简单。

That being said, your .swap()one is not a copy, instead it swaps the two vectors. You would modify the original to not contain anything anymore! Which is not a copy.

话虽如此,您.swap()的不是副本,而是交换了两个向量。您将修改原始内容以不再包含任何内容!这不是副本。

回答by Jerry Yang

Direct answer:

直接回答:

  • Use a =operator
  • 使用=运算符

We can use the public member function std::vector::operator=of the container std::vectorfor assigning values from a vector to another.

我们可以使用std::vector::operator=容器的公共成员函数std::vector将值从一个向量分配给另一个向量。

  • Use a constructor function
  • 使用构造函数

Besides, a constructor function also makes sense. A constructor function with another vector as parameter(e.g. x) constructs a container with a copy of each of the elements in x, in the same order.

此外,构造函数也有意义。以另一个向量作为参数(例如x)的构造函数以x相同的顺序构造一个容器,其中包含 中每个元素的副本。

Caution:

警告:

  • Do not use std::vector::swap
  • 不使用 std::vector::swap

std::vector::swapis not copyinga vector to another, it is actually swapping elements of two vectors, just as its name suggests. In other words, the source vector to copy from is modified after std::vector::swapis called, which is probably not what you are expected.

std::vector::swap不是向量复制到另一个向量,它实际上是交换两个向量的元素,正如其名称所暗示的那样。换句话说,要复制的源向量在std::vector::swap被调用后被修改,这可能不是您所期望的。

  • Deep or shallow copy?
  • 深拷贝还是浅拷贝?

If the elements in the source vector are pointers to other data, then a deep copy is wanted sometimes.

如果源向量中的元素是指向其他数据的指针,则有时需要深拷贝。

According to wikipedia:

根据维基百科:

A deep copy, meaning that fields are dereferenced: rather than references to objects being copied, new copy objects are created for any referenced objects, and references to these placed in B.

深度复制,意味着字段被取消引用:不是对正在复制的对象的引用,而是为任何被引用的对象创建新的副本对象,并将对这些对象的引用放置在 B 中。

Actually, there is no currently a built-in way in C++ to do a deep copy. All of the ways mentioned above are shallow. If a deep copy is necessary, you can traverse a vector and make copy of the references manually. Alternatively, an iterator can be considered for traversing. Discussion on iterator is beyond this question.

实际上,C++ 中目前没有内置的方法来进行深度复制。上面提到的所有方式都是肤浅的。如果需要深度复制,您可以遍历向量并手动复制引用。或者,可以考虑使用迭代器进行遍历。关于迭代器的讨论超出了这个问题。

References

参考

The page of std::vectoron cplusplus.com

页面std::vector上cplusplus.com

回答by FaridLU

new_vector.assign(old_vector.begin(),old_vector.end()); // Method 1
new_vector = old_vector; // Method 2

回答by Raz

you should not use swap to copy vectors, it would change the "original" vector.

你不应该使用交换来复制向量,它会改变“原始”向量。

pass the original as a parameter to the new instead.

将原始作为参数传递给新的。

回答by sgowd

In case the vector ALREADY existed and you wanted to just copy, you could do this:

如果向量已经存在并且您只想复制,您可以这样做:

newVec.resize(oldVec.size());
memcpy(&newVec.at(0), &oldVec.at(0), oldVec.size());