通过保留和复制来复制向量,还是通过创建和交换来复制向量更有效?
我正在尝试有效地复制向量。 我看到两种可能的方法:
std::vector<int> copyVecFast1(const std::vector<int>& original)
{
std::vector<int> newVec;
newVec.reserve(original.size());
std::copy(original.begin(), original.end(), std::back_inserter(newVec));
return newVec;
}
std::vector<int> copyVecFast2(std::vector<int>& original)
{
std::vector<int> newVec;
newVec.swap(original);
return newVec;
}
首选哪种方法,为什么? 我正在寻找最有效的解决方案,以避免不必要的复制。
I am trying to efficiently make a copy of a vector. I see two possible approaches:
std::vector<int> copyVecFast1(const std::vector<int>& original)
{
std::vector<int> newVec;
newVec.reserve(original.size());
std::copy(original.begin(), original.end(), std::back_inserter(newVec));
return newVec;
}
std::vector<int> copyVecFast2(std::vector<int>& original)
{
std::vector<int> newVec;
newVec.swap(original);
return newVec;
}
Which of these is preferred, and why? I am looking for the most efficient solution that will avoid unnecessary copying.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
但它们并不相同,不是吗? 一个是副本,另一个是交换。 因此就有了函数名称。
我最喜欢的是:
其中
a
和b
是向量。They aren't the same though, are they? One is a copy, the other is a swap. Hence the function names.
My favourite is:
Where
a
andb
are vectors.如果您通过引用发送参数,您的第二个示例将不起作用。 你的意思是
那会起作用,但更简单的方法是
Your second example does not work if you send the argument by reference. Did you mean
That would work, but an easier way is
这是复制向量的另一种有效方法,只需使用它的构造函数:
这比使用 std::copy 将整个向量从头到尾遍历到 std 还要简单: :back_insert 将它们插入到新向量中。
话虽如此,您的
.swap()
不是副本,而是交换两个向量。 您将修改原始内容以不再包含任何内容! 这不是副本。This is another valid way to make a copy of a vector, just use its constructor:
This is even simpler than using
std::copy
to walk the entire vector from start to finish tostd::back_insert
them into the new vector.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.直接答案:
=
运算符我们可以使用容器
std::vector
的公共成员函数std::vector::operator=
用于将一个向量的值分配给另一个向量。此外,构造函数也很有意义。 以另一个向量作为参数(例如
x
)的构造函数构造一个容器,其中包含x
中每个元素的副本(按相同顺序)。注意:
std::vector::swap
std::vector::swap
不是复制一个向量到另一个向量,顾名思义,它实际上是交换两个向量的元素。 换句话说,要复制的源向量在调用 std::vector::swap 后被修改,这可能不是您所期望的。如果源向量中的元素是指向其他数据的指针,则有时需要深层复制。
根据维基百科:
实际上,当前 C++ 中没有内置方法来做一个深拷贝。 上面提到的所有方法都是肤浅的。 如果需要深层复制,您可以遍历向量并手动复制引用。 或者,可以考虑使用迭代器进行遍历。 关于迭代器的讨论超出了这个问题。
参考文献
cplusplus.com 上的
std::vector
页面Direct answer:
=
operatorWe can use the public member function
std::vector::operator=
of the containerstd::vector
for assigning values from a vector to another.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 inx
, in the same order.Caution:
std::vector::swap
std::vector::swap
is not copying a 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 afterstd::vector::swap
is called, which is probably not what you are expected.If the elements in the source vector are pointers to other data, then a deep copy is wanted sometimes.
According to wikipedia:
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.
References
The page of
std::vector
on cplusplus.com你不应该使用交换来复制向量,它会改变“原始”向量。
将原始数据作为参数传递给新数据。
you should not use swap to copy vectors, it would change the "original" vector.
pass the original as a parameter to the new instead.