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
Copying std::vector: prefer assignment or std::copy?
提问by Violet Giraffe
I have two vectors:
我有两个向量:
std::vector<int> v1, v2;
// Filling v1
...
And now I need to copy v1
to v2
. Is there any reason to prefer
现在我需要复制v1
到v2
. 有什么理由喜欢
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
:
- It is shorter and makes the intend more clear
std::copy
won't work ifv2
doesn't have the same length asv1
(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- If
v1
is about to expire (and you use C++11) you can easily modify it tomove
the contents - Performancewise assignment is unlikely to be slower then
std::copy
, since the implementers would probably usestd::copy
internally, if it gave a performance benefit.
- 它更短,使意图更清晰
std::copy
如果v2
不具有相同的长度v1
(它不会调整它的大小,因此它将保留一些旧元素最好的情况(v2.size() > v1.size()
并覆盖程序最坏情况下其他地方使用的一些随机数据)将不起作用- 如果
v1
即将过期(并且您使用 C++11)您可以轻松地将其修改为move
内容 - Performancewise 分配不太可能比 慢
std::copy
,因为实现者可能会在std::copy
内部使用,如果它提供了性能优势。
In conclusion, std::copy
is 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::copy
may 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 v2
isn't big enough you'll get a buffer overrun if you use copy
as 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 v1
is.
您可以使用将调用 push_back on 的后插入迭代器v2
。但是,这可能会导致多次重新分配,具体取决于大小v1
。
copy(v1.begin(), v1.end(), back_inserter(v2));
You're better off letting vector
manage 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::copy
would be appropriate is if you are only replacing a small range totally within the vector.
任务,到目前为止。更一般地,任何时候向量的大小可能改变,或改变向量的全部内容,你应该更喜欢成员函数。唯一std::copy
合适的时间是如果您只是完全替换向量内的一个小范围。
回答by Symaxion
It's shorter.
它更短。
std::copy
is 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_copy
depending size and capacity) or so performances are the same.
分配更清晰,内部使用std::copy
(或unitizalized_copy
_M_allocate_and_copy
取决于大小和容量)或因此性能相同。