复制vector和赋值vector的区别是什么?
What is difference between copy and assigning a vector?
复制和赋值的区别是什么?第2、4行
1 vector<int> V1(5);
2 vector<int> V3(V1);
3 vector<int> V4(V1.size());
4 V4 = V1 ;
这是我对operator=
的stl实现的氧气提取:
/* 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.
*/
正如您所看到的,如果使用自定义分配器,结果会有所不同,但在其他情况下,结果是相同的。
第2行使用了复制构造函数。第4行使用了复制赋值。两者都创建副本;第一个命令创建一个新对象,第二个命令覆盖一个现有对象。
相关文章:
- 如果 KEY 是 std::list 或 std::vector 而不是值,那么 std::map 的默认行为是什么?
- 在自定义 std::vector-like 容器中处理指针和非指针模板类型的最佳方法是什么?
- 向后迭代 std::array 或 std::vector 的正确方法是什么?
- 这句话是什么意思 - " vector<long long> distance(n, std::numeric_limits<long long>::max()); "?
- 最接近"std::atomic<std::vector>"是什么?
- 行 - " vector<int> used(adj.size(), 0); "是什么意思?
- 将vector<vector<double>>从x86平台中创建的一个进程发送到x64中构建的另一个进程的最快方法是什么
- 使输出流式处理运算符适用于 boost::variant<std::vector<int>、int、double 的正确方法是什么>
- 代码中的"vector<<int>vector>&index"是什么意思?
- 释放 std::vector 中指针内存的最有效方法是什么?
- 这个(const vector<vector<int> > &A)"代码段是什么意思?
- C 11中的标准方法是什么,可以访问std :: vector中元素n的指针
- 当编译器看到 std::vector<Typo> 和 std::vector<struct 拼写错误时发出的诊断之间的差异背后的理由是什么>
- 如果我们可以使用 vector[1] = someInt,emplace() 的目的是什么
- C++中的"vector<type[n]>"是什么?
- 当第一个 itr 在最后一个 itr 之后时,std::vector 范围构造函数的官方行为是什么?
- 在OpenCV中,std::vector而不是cv::Mat的"type"是什么解释,我该如何更改它?(C++)
- 复制包含STL成员(如vector)的C++结构的最佳方法是什么
- 这个代码是什么意思 std::vector<cv::P oint2f> points[2]
- glm 的 oriented Angle() 函数中的"reference vector"是什么?