简体   繁体   English

vector :: erase会减少vector :: capacity吗?

[英]Does vector::erase reduce vector::capacity?

Cppreference only says: Cppreference只说:

Removes specified elements from the container. 从容器中删除指定的元素。 1) Removes the element at pos. 1)删除pos处的元素。 2) Removes the elements in the range [first; 2)删除范围内的元素[first; last). 持续)。

Invalidates iterators and references at or after the point of the erase, including the end() iterator. 在擦除点或之后使迭代器和引用无效,包括end()迭代器。

The iterator pos must be valid and dereferenceable. 迭代器pos必须是有效且可解除引用的。 Thus the end() iterator (which is valid, but is not dereferencable) cannot be used as a value for pos. 因此,end()迭代器(有效但不可解除引用)不能用作pos的值。

The iterator first does not need to be dereferenceable if first==last: erasing an empty range is a no-op. 如果first == last,迭代器首先不需要是可解除引用的:擦除空范围是无操作。

Not necessarily no. 不一定没有。 When reading the C++ standard (and cppreference proxies the standard remarkably well), if something is not explicitly mentioned, then assume such a something is not required. 当阅读C ++标准(和cppreference代表标准非常好)时,如果没有明确提到某些内容,那么假设不需要这样的东西。

It would possibly be sub-optimal for a C++ Standard Library implementation to do so. 对于C ++标准库实现来说,这可能是次优的。

No. That's implied by the fact that iterators, pointers and references prior to the point of erase remain valid. 不。这是因为擦除点之前的迭代器,指针和引用仍然有效。 Reducing the capacity would require a reallocation. 减少容量需要重新分配。

With other constraints as complexity or iterator validity, that might force implementation in some way. 使用其他约束作为复杂性或迭代器有效性,可能会以某种方式强制实现。

Here: 这里:

Complexity: 复杂:
Linear on the number of elements erased (destructions) plus the number of elements after the last element deleted (moving). 删除的元素数量(destructions)加上最后一个元素删除(移动)后的元素数量。

So decreasing capacity and moving old objects is a resized buffer is not possible. 因此,减少容量和移动旧对象是一个调整大小的缓冲区是不可能的。 Except in the case when first and begin are the same. 除了在firstbegin是相同的情况。

Decreasing just the capacity number would be possible, but I don't see any sane implementation doing that. 只能减少容量数量是可能的,但我没有看到任何理智的实现。

The only vector operations that affect the capacity are the ones that invalidate all iterators, pointers and references, because they have reallocated the storage and moved the elements. 影响容量的唯一向量操作是使所有迭代器,指针和引用无效的操作,因为它们已重新分配存储并移动元素。

There is nothing in the Allocator concept that would allow it to change the allocation size in place, so the capacity can't change in that manner either. Allocator概念中没有任何内容可以允许它在适当的位置更改分配大小,因此容量也不能以这种方式改变。

In theory an implementation could specialise on std::allocator and reallocate, under the "As-if" rule, but I doubt there are any serious implementations that would do such a thing. 理论上,一个实现可以专注于std::allocator和reallocate,在“As-if”规则下,但我怀疑有任何严重的实现可以做这样的事情。

A quick example could have cleared your doubt :( compiled using VS2017 ) 一个简单的例子可以清除你的疑问:(使用VS2017编译)

#include <iostream>
#include <vector>

int main()
{
    std::vector<int> intVec{ 1,2,3,4,5,6,7,8,9,10 };
    std::cout << "Capacity and size before erase : " << intVec.capacity() << ", "<< intVec.size() << std::endl;
    intVec.erase(intVec.begin() + 3);
    std::cout << "Capacity and size after erase : " << intVec.capacity() << ", " << intVec.size() << std::endl;
    return 0;
}

Output : 输出:

Capacity and size before erase : 10, 10 擦除前的容量和大小:10,10

Capacity and size after erase : 10, 9 擦除后的容量和大小:10,9

Note that while capacity() doesn't decrease after the erase, the size() certainly does. 请注意,虽然在擦除后capacity()没有减少,但size()肯定会减少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM