m_listItems.capacity()) m_listItems.reserve(count + ...">

Changing backup memory to C ++ vector

I have a vector with 1000 "nodes"

 if(count + 1 > m_listItems.capacity())
     m_listItems.reserve(count + 100);

The problem is that I also clarify this when I am going to replenish it.

m_listItems.clear();

Capacity does not change. I used resizing (1); but this does not seem to change the possibility. So how to change the reserve?

+5
source share
5 answers
vector<Item>(m_listItems).swap(m_listItems);

hiding again m_listItems: http://www.gotw.ca/gotw/054.htm (Herb Sutter)

If you still want to clear it, replace it with an empty vector:

vector<Item>().swap(m_listItems);

, , . ( , . )

+20

, , http://www.gotw.ca/gotw/054.htm, , , ' , , , . ( , , )

, , , :

std::vector<foo> v(1000); // Create a vector with capacity for 1000 elements

, , ? ( std::vector (v).swap(v)), ?

, , , .

Edit:

baash05: , 1000000 10 . ?

. , , , , . ( , , ). ( ), , , , RAM.

1000000 , .

, , , . , , , , , , .

. , , . , , , , , , , .

+2

std::vector< int > v;
// ... fill v with stuff...
std::vector< int >().swap( v );
+1

swap , .

vector< int > tmp;
old.swap( tmp );
+1

, , -; . ; , . , , . , .

+1

All Articles