容量是否复制在向量中? [英] Is capacity copied in a vector?

查看:284
本文介绍了容量是否复制在向量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请使用以下代码:

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 中没有提供复制构造函数的定义, / a>。相反,我们必须查看 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
b 表示 X u 表示标识符, r 表示
a类型<$ c $的非常量值c> X , rv 表示
类型 X

X denotes a container class containing objects of type T, a and b denote values of type X, u denotes an identifier, r denotes a non-const value of type X, and rv denotes a non-const rvalue of type X.

X u(a)

X u = a;

需要: T CopyInsertable X (见下文)。

post: u == a


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.

这篇关于容量是否复制在向量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆