Changing the reserve memory of C++ vector

前端 未结 5 1347
情深已故
情深已故 2021-01-06 00:29

I have a vector with 1000 \"nodes\"

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

The problem is I also

5条回答
  •  孤街浪徒
    2021-01-06 01:15

    As far as I can tell, you can't reallocate a vector to a lower capacity than it ever has; you can only allocate it larger. There are good reasons for this; among them is that the reallocation process is hugely computationally intensive. If you really need to have a smaller vector, free the old one and create a new one that's smaller. That's actually computationally much simpler than having the vector actually resize smaller.

提交回复
热议问题