vector using the swap () function to release memory

First, vector and deque different, the memory footprint will increase, not decrease. For example, you first allocate 10,000 bytes,
then erase off back to 9,999, although it is only one element of an effective, but the memory is still occupied by 10,000. All recovered at the space vector destructor.

empty () is used to detect whether a container is empty, clear () can clear all elements.
But even clear (), the amount of memory space remains the same. If you need to reduce the dynamic space, consider using deque.
If you have to use vector, there is a way:
the premise of using this method is vector former store a lot of data, such as 10000000, which after various treatments,
there are only 100, then the original data to the empty space occupied,
it this switching technique can swap technique
is by exchanging function swap (), out of its own so that the vector scope,
thereby forcing the release of the memory space occupied by the vector.

. 1 Vector < int > a, B;
 2  for ( int I = . 1 ; I <= 10 ; ++ I) a.push_back (I);
 . 3 a.swap (B); // space was of such a released

But in fact, tell the truth, this is not what the eggs with, rarely used

Guess you like

Origin www.cnblogs.com/adelalove/p/11716297.html