C++ 复制 std::vector:更喜欢赋值还是 std::copy?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14977632/
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 18:51:31  来源:igfitidea点击:

Copying std::vector: prefer assignment or std::copy?

c++stlcopy

提问by Violet Giraffe

I have two vectors:

我有两个向量:

std::vector<int> v1, v2;

// Filling v1
...

And now I need to copy v1to v2. Is there any reason to prefer

现在我需要复制v1v2. 有什么理由喜欢

v2 = v1;

v2 = v1;

to

std::copy (v1.begin(), v1.end(), v2.begin());

std::copy (v1.begin(), v1.end(), v2.begin());

(or vice versa)?

(或相反亦然)?

回答by Grizzly

Generally I would strongly prefer v2 = v1:

一般来说,我强烈希望v2 = v1

  1. It is shorter and makes the intend more clear
  2. std::copywon't work if v2doesn't have the same length as v1(it won't resize it, so it will retain some of the old elements best case (v2.size() > v1.size()and overwrite some random data used elsewhere in the program worst case
  3. If v1is about to expire (and you use C++11) you can easily modify it to movethe contents
  4. Performancewise assignment is unlikely to be slower then std::copy, since the implementers would probably use std::copyinternally, if it gave a performance benefit.
  1. 它更短,使意图更清晰
  2. std::copy如果v2不具有相同的长度v1(它不会调整它的大小,因此它将保留一些旧元素最好的情况(v2.size() > v1.size()并覆盖程序最坏情况下其他地方使用的一些随机数据)将不起作用
  3. 如果v1即将过期(并且您使用 C++11)您可以轻松地将其修改为move内容
  4. Performancewise 分配不太可能比 慢std::copy,因为实现者可能会在std::copy内部使用,如果它提供了性能优势。

In conclusion, std::copyis less expressive, might do the wrong thing and isn't even faster. So there isn't really any reason to use it here.

总之,std::copy表达能力较差,可能会做错事,甚至不会更快。所以真的没有任何理由在这里使用它。

回答by Cheers and hth. - Alf

The invocation of std::copymay try to access items beyond the end of the destination vector.

的调用std::copy可能会尝试访问超出目标向量末尾的项目。

Use assignment.

使用赋值。

It's not your job to micro-optimize: that's the library writer's responsibility, and ultimately the compiler's responsibility.

微优化不是你的工作:这是库作者的责任,最终是编译器的责任。

You can make your code arbitrarily fastif it doesn't have to be correct.

如果不需要正确,您可以任意快速地编写代码。

In the case of the copy, however, it's rather doubtful whether it even is faster, and it's certainly not correct for the general case.

copy然而,在 的情况下,它是否更快是相当值得怀疑的,并且对于一般情况肯定是不正确的。

回答by Peter Wood

If v2isn't big enough you'll get a buffer overrun if you use copyas you have.

如果v2不够大,如果copy按原样使用,则会导致缓冲区溢出。

You can use a back insert iterator which will call push_back on v2. However this could lead to multiple reallocations depending upon how big v1is.

您可以使用将调用 push_back on 的后插入迭代器v2。但是,这可能会导致多次重新分配,具体取决于大小v1

copy(v1.begin(), v1.end(), back_inserter(v2));

You're better off letting vectormanage things correctly. The assignment operator does this, as does vector::assign:

你最好让vector事情正确管理。赋值运算符执行此操作,如下所示vector::assign

v2.assign(v1.begin(), v1.end());

I have an inkling that the assignment operator is implemented in terms of vector::assign.

我有一种暗示,赋值运算符是根据vector::assign.

回答by James Kanze

Assignment, by far. More generally, any time the size of the vector might change, or change the entire contents of the vector, you should prefer member functions. The only time std::copywould be appropriate is if you are only replacing a small range totally within the vector.

任务,到目前为止。更一般地,任何时候向量的大小可能改变,或改变向量的全部内容,你应该更喜欢成员函数。唯一std::copy合适的时间是如果您只是完全替换向量内的一个小范围。

回答by Symaxion

It's shorter.

它更短。

std::copyis mainly meant for copying sections of containers. If you need to copy an entire container, you might as well use the copy constructor.

std::copy主要用于复制容器的部分。如果需要复制整个容器,不妨使用复制构造函数。

回答by FredericS

Assignement is clearer and internally uses std::copy(or unitizalized_copy_M_allocate_and_copydepending size and capacity) or so performances are the same.

分配更清晰,内部使用std::copy(或unitizalized_copy_M_allocate_and_copy取决于大小和容量)或因此性能相同。