本文介绍了在向量复制/移动分配时底层存储会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于std :: vector的副本分配,是当源的大小小于目标的容量时,容量的重新分配和容量的缩减吗?或者是否保证重新分配/收缩不会发生(即始终遵守先前的reserve())?

For std::vector's copy assignment, is reallocation of storage and shrink of capacity allowed when the source's size is smaller than the destination's capacity? Or is it guaranteed that the reallocation/shrink will not happen (i.e. always respect previous reserve())?

另一方面,如果源的大小大于目的地的容量和重新分配是否发生,是否需要重新分配遵守源的容量(例如,目的地的新容量应该不小于源的容量,或者甚至要求它们相同)。或者,重新分配只是简单地根据新的大小来做它的工作,而不考虑源的容量?

On the other side, if the source's size is bigger than the destination's capacity and a reallocation takes place, is it required that the reallocation respect the source's capacity (e.g. the destination's new capacity should be no smaller than the source's capacity, or even require them to be the same)? Or the reallocation simply does its job (based on the new size) with no regard to the source's capacity?

对于移动分配,我认为不会发生存储重新分配(虽然我没有找到标准中的相关部分),那么是否意味着目的地的新容量的价值将与源的旧容量完全相同?我可以期望 v = vector {}; 具有与相同的效果vector< T> {}。swap(v) / code>?

As for move assignment, I suppose no storage reallocation will take place (though I failed to locate relevant part in the standard), so does it mean the value of the destination's new capacity will be exactly the same to the source's old capacity? Can I expect v = vector<T>{}; to have the same effect as vector<T>{}.swap(v);?

我认为答案埋在标准的某个地方,但我没有找到它们。
(如果C ++ 11和C ++ 03的情况不同,我想知道两种情况下的各种需求。)

I suppose the answers are buried somewhere in the standard, but I just failed to find them.(In case things are different for C++11 and C++03, I would like to know various requirements from both.)

PS:For无论什么答案以上问题,是相同的std :: string(只在C ++ 11意味着连续存储和没有COW,C ++ 03字符串是在雷达)?

PS: For whatever answer of the above questions, is it the same for std::string (only in C++11 which means contiguous storage and no COW, C++03 string is out of the radar)?

推荐答案

我可以在标准中找到任何东西,这将允许分配
a向量到一个具有足够的容量来减少容量。
如果我在分配之前做了 reserve ,我保证
,只要$ b重新分配,迭代器就不会失效$ b向量永远不会大于我保留的容量。

I can find nothing in the standard which would allow assigninga vector to one with sufficient capacity to reduce the capacity.If I've done reserve before the assignment, I'm guaranteedthat iterators won't be invalidated by a reallocation as long asthe vector never becomes larger than the capacity I reserved.

移动赋值的问题是特别的。没有
似乎是任何特殊情况下允许它无效迭代器
(除非源大于
目标的容量),但这种方式打败了目标移动
赋值。我怀疑这是标准中的缺陷。

The issue with move assignment is particular. There doesn'tseem to be any special case to allow it to invalidate iterators(unless the source is larger than the capacity of thedestination), but this sort of defeats the goal of moveassignment. I suspect that this is a defect in the standard.

编辑:

对于值得的,在表96中, a = rv (其中 a
a容器, rv 相同类型的
容器的const r值),标准给出线性复杂度,并说
所有现有元素a a 移动分配到或
销毁。很明显,标准的意图是 ;
move的执行时间优点只适用于单个元素,而不适用于容器
本身。

For what it's worth, in table 96, for a = rv (where a isa container, and rv is a non-const r-value of the same type ofcontainer), the standard gives linear complexity, and says that"all existing elements a a are either move assigned to ordestroyed". So apparently, the intent of the standard is forthe capacity not to be reduced; the execution-time benefits ofmove only apply to the individual elements, not to the containeritself.

这篇关于在向量复制/移动分配时底层存储会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 14:12