简体   繁体   English

std :: vector向下调整大小

[英]std::vector resize downward

The C++ standard seems to make no statement regarding side-effects on capacity by either resize(n) , with n < size() , or clear() . C ++标准似乎没有通过resize(n)n < size()clear()来声明容量的副作用。

It does make a statement about amortized cost of push_back and pop_back - O(1) 它确实声明了关于push_backpop_back摊销成本 - O(1)

I can envision an implementation that does the usual sort of capacity changes ala CLRS Algorithms (eg double when enlarging, halve when decreasing size to < capacity()/4 ). 我可以设想一种实现通常的容量变化和CLRS算法(例如,当放大时加倍,当将size to < capacity()/4减小size to < capacity()/4时减半)。 (Cormen Lieserson Rivest Stein) (Cormen Lieserson Rivest Stein)

Does anyone have a reference for any implementation restrictions? 有没有人参考任何实施限制?

Calling resize() with a smaller size has no effect on the capacity of a vector . 调用较小大小的resize()vector的容量没有影响。 It will not free memory. 它不会释放内存。

The standard idiom for freeing memory from a vector is to swap() it with an empty temporary vector : std::vector<T>().swap(vec); vector释放内存的标准习惯是将它与一个空的临时vector swap()std::vector<T>().swap(vec); . If you want to resize downwards you'd need to copy from your original vector into a new local temporary vector and then swap the resulting vector with your original. 如果要向下调整大小,则需要从原始向量复制到新的本地临时向量,然后将生成的向量与原始向量交换。

Updated: C++11 added a member function shrink_to_fit() for this purpose, it's a non-binding request to reduce capacity() to size() . 更新:为此目的,C ++ 11添加了一个成员函数shrink_to_fit() ,它是一个将capacity()减小到size()的非绑定请求。

Actually, the standard does specify what should happen: 实际上,该标准确实指明了应该发生的事情:

This is from vector , but the theme is the same for all the containers ( list , deque , etc...) 这是来自vector ,但主题对于所有容器都是相同的( listdeque等...)

23.2.4.2 vector capacity [lib.vector.capacity] 23.2.4.2向量容量[lib.vector.capacity]

void resize(size_type sz, T c = T());

6) Effects: 6)效果:

if (sz > size())
    insert(end(), sz-size(), c);
else if (sz < size())
    erase(begin()+sz, end());
else
    ; //do nothing

That is to say: If the size specified to resize is less than the number of elements, those elements will be erased from the container. 也就是说:如果指定resize小于元素数,则这些元素将从容器中删除。 Regarding capacity() , this depends on what erase() does to it. 关于capacity() ,这取决于erase()对它的作用。

I cannot locate it in the standard, but I'm pretty sure clear() is defined to be: 我无法在标准中找到它,但我很确定clear()被定义为:

void clear()
{
    erase(begin(), end());
}

Therefore, the effects clear() has on capacity() is also tied to the effects erase() has on it. 因此, clear()capacity()的影响也与erase()对其产生的影响有关。 According to the standard: 根据标准:

23.2.4.3 vector modifiers [lib.vector.modifiers] 23.2.4.3向量修饰符[lib.vector.modifiers]

iterator erase(iterator position);
iterator erase(iterator first, iterator last);

4) Complexity: The destructor of T is called the number of times equal to the number of the elements erased.... 4)复杂性:T的析构函数被称为等于被删除元素数量的次数....

This means that the elements will be destructed, but the memory will remain intact. 这意味着元素将被破坏,但内存将保持不变。 erase() has no effect on capacity, therefore resize() and clear() also have no effect. erase()对容量没有影响,因此resize()clear()也没有效果。

The capacity will never decrease. 容量永远不会减少。 I'm not sure if the standard states this explicitly, but it is implied: iterators and references to vector's elements must not be invalidated by resize(n) if n < capacity() . 我不确定标准是否明确说明了这一点,但暗示:如果n < capacity()则不能通过resize(n)使迭代器和向量元素的引用无效。

As i checked for gcc (mingw) the only way to free vector capacity is what mattnewport says. 当我检查gcc(mingw)时,释放矢量容量的唯一方法就是mattnewport所说的。 Swaping it with other teporary vector. 用其他teporary向量交换它。 This code makes it for gcc. 这段代码适用于gcc。

template<typename C> void shrinkContainer(C &container) {
    if (container.size() != container.capacity()) {
        C tmp = container;
        swap(container, tmp);
    }
    //container.size() == container.capacity()
}

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

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