Why not resize and clear jobs in the GotW 54?

Referring to the Gotw 54 article from HerbSutter, he explains

  • The right way to compress-paste a vector or deque and

  • The right way to completely clear a vector or deque

Can I use container.resize()it container.clear()for the above task or am I missing something?

+3
source share
3 answers

, : size Vs capacity. resize , , ( ) . resize - , , , .

, .

size     == how much you are using
capacity == how much memory is reserved
vector<int> v(10);

v.resize(5); // size == 5 but capacity (may or may) not be changed
v.clear()    // size == 0 but capacity (may or may) not be changed

, , / . , " , .

+6

resize(), clear() . .capacity() , , , size() , , reserve() d. , . capacity() , , size() reserve().

.capacity() , . . , . .

, , . . -, , . , , 2 .

+3

. X, Y . , ( ) : , .

+1

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


All Articles