Is space redistributed in vector :: resize () when resizing to a lower count?

According to Stroustrup: C ++ programming language : -

"When you resize vector to accommodate more (or fewer) elements, all of its elements can be moved to new places."

Is this true even if the vector is resized to a smaller size?

+2
source share
4 answers

1: std::vector::capacity(), .
2: std::vector::capacity(), .


Standerdese:

vector::resize() :

++ 11 23.3.6.3/12:

void resize(size_type sz, const T& c);

:

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

@DavidRodríguez-dribeas , Iterator std::vector::insert():

23.3.6.5

1 [insert, push_back, emplace, emplace_back]

: , . , .

: , , , , , / . resize() / [ 1]. .

, 1.

2 std::vector::erase(), :

23.3.6.5

( const_iterator);

3 : .

[ 1] , .

+4

... . "

, , . , , , .

+1

. / , (1) (2), . - capacity().

resize() / . , , , . , , , capacity(), , .

Als 1 :

23.3.6.5

1 [insert, push_back, emplace, emplace_back]

: , . , .

2 []

: .

++ 03.


1 , resize . .

+1

" ( ) , ." "

0

Source: https://habr.com/ru/post/1662470/


All Articles