复制和分配向量有什么区别?第 2 行和第 4 行。
1 vector<int> V1(5);
2 vector<int> V3(V1);
3 vector<int> V4(V1.size());
4 V4 = V1 ;
复制和分配向量有什么区别?第 2 行和第 4 行。
1 vector<int> V1(5);
2 vector<int> V3(V1);
3 vector<int> V4(V1.size());
4 V4 = V1 ;
第 2 行使用复制构造函数。第 4 行使用复制分配。两者都创建副本;第一个创建一个新对象,第二个覆盖现有对象。
这是我的 stl 实现中的 doxygen 提取物operator=
:
/* All the elements of @a x are copied, but any extra memory in
* @a x (for fast expansion) will not be copied. Unlike the
* copy constructor, the allocator object is not copied.
*/
如您所见,如果您使用自定义分配器会有所不同,但在其他情况下结果是相同的。