Is capacity copied in a vector?(容量是否复制到向量中?)
问题描述
输入以下代码:
std::vector<int> a;
a.reserve(65536);
std::vector<int> b(a); //NOTE: b is constructed from a
a.reserve(65536); // no reallocation
b.reserve(65536);
是否复制了容量?最后一行会重新分配吗?标准对此是否有任何说明或沉默?
Is capacity copied? Will there be a reallocation on the last line? Does the standard say anything about this or is it silent?
推荐答案
是否复制了容量?
Is capacity copied?
在实践中,没有.我在 Clang 和 GCC 以及 MSVC 并且它们都没有复制容量.
In practice, no. I tested it online in Clang and GCC as well as MSVC and none of them copy the capacity.
最后一行会重新分配吗?
Will there be a reallocation on the last line?
如果容量小于要保留的参数(即它不会被复制),则是.
If the capacity is less than the argument to reserve (i.e. it doesn't get copied) then yes.
标准对此有说明还是沉默?
Does the standard say anything about this or is it silent?
vector.cons 中没有提供复制构造函数的定义.相反,我们必须查看 container.requirements
No definitions for the copy constructor are provided in vector.cons. Instead we have to look at the container.requirements
X 表示包含 T、a 和 a 类型对象的容器类b 表示 X 类型的值,u 表示一个标识符,r 表示X 类型的非常量值,并且 rv 表示的非常量值输入 X.
Xdenotes a container class containing objects of typeT,aandbdenote values of typeX,udenotes an identifier,rdenotes a non-const value of typeX, andrvdenotes a non-const rvalue of typeX.
X u(a)
X u = a;
要求: T 是 CopyInsertable 到 X(见下文).
Requires: T is CopyInsertable into X (see below).
发布:u == a
现在两个容器相等是什么意思?
Now what does it mean for two containers to be equal?
a == b
== 是一个等价关系.equal(a.begin(), a.end(), b.begin(), b.end())
换句话说,既然不需要capacity在比较中相等,那么就没有理由复制capacity.
In other words, since it doesn't require capacity to be equal in the comparison, then there's no reason to copy the capacity.
这篇关于容量是否复制到向量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:容量是否复制到向量中?
基础教程推荐
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- CString 到 char* 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
