Делает станд.:: вектор pop_back () изменяет способность вектора?

Этот вопрос решает очень связанную проблему, которая может помочь сетевые ресурсы использования в powershell.

14
задан unwind 8 October 2009 в 10:15
поделиться

4 ответа

No. The only way to shrink a vector's capacity is the swap trick

template< typename T, class Allocator >
void shrink_capacity(std::vector<T,Allocator>& v)
{
   std::vector<T,Allocator>(v.begin(),v.end()).swap(v);
}

and even that isn't guaranteed to work according to the standard. (Although it's hard to imagine an implementation where it wouldn't work.)

As far as I know, the next version of the C++ standard (what used to be C++0x, but now became C++1x) will have std::vector<>::shrink_to_fit().

19
ответ дан 1 December 2019 в 08:53
поделиться

No. pop_back() will not shrink the capacity of vector. use std::vector(v).swap(v) instead.

4
ответ дан 1 December 2019 в 08:53
поделиться

NO. Same as push_back , pop_back won't impact the capacity(). They just impact the size().

EDIT:

I should have said push_back won't change the capacity when the v.size() < v.capacity().

1
ответ дан 1 December 2019 в 08:53
поделиться

pop_XXX никогда не изменит емкость. push_XXX может изменить емкость, если вы попытаетесь протолкнуть больше материала, чем позволяет емкость.

2
ответ дан 1 December 2019 в 08:53
поделиться
Другие вопросы по тегам:

Похожие вопросы: