Correctly release the memory of vector: clear or swap?

1. The difference between size() and capacity() methods

1. Vector has size() and capacity() methods, both of which are used to get the size of vector, so what is the difference between them?

Let's look at a piece of code first:

int main() {

    std::vector<int> v1;
    std::cout <<"size:"<< v1.size() << " ,capacity:" << v1.capacity() << std::endl;
    v1.resize(10);
    std::cout <<"size:"<< v1.size() << " ,capacity:" << v1.capacity() << std::endl;

    for (int i = 0; i < 10; i++) {
        std::cout << v1[i] << " ";
    }
    std::cout<<std::endl;
    v1.push_back(1);
    std::cout << "size:" << v1.size() << " ,capacity:" << v1.capacity() << std::endl;

    return 0;
}

The execution results are as follows:

Here are the following conclusions:

(1) capacity() indicates the pre-allocated memory space of the vector, and indicates the maximum number of elements that the vector can hold.

(2) size() indicates the number of elements actually occupied in the vector.

We saw size=11, capacity=15 above, now we print the 12th and 13th elements to see what the result is:

std::cout << v1[11] << " " << v1[12] << std::endl;

It can be seen that the pre-allocated space is also accessible, but the value inside is uncertain.

Second, the difference between the resize() and reserve() methods

(1) Using resize(), the object memory space in the container really exists.

In the above example, we see that after executing: v1.resize(10);, the values ​​of size() and capacity() are both 10.

(2) reserve() only modifies the value of capacity, and the objects in the container do not have real memory space (the space is "wild").

3. The difference between clear() and swap()

(1) Only releasing (destructing) the elements in the container will result in size = 0, but the capacity of the container will not change, that is, the occupied memory will not change.

Let's verify it with the following code:

 v1.clear();
 std::cout << "size:" << v1.size() << " ,capacity:" << v1.capacity() << std::endl;

Results of the:

It can be seen that size() becomes 0 after clear(), but the value of capacity() is still 15.

(2) Verify the swap() method

//v1.clear();
std::vector<int> tmp;
tmp.swap(v1);
std::cout << "size:" << v1.size() << " ,capacity:" << v1.capacity() << std::endl;

 The result is as follows:

The container that needs to be shrunk is exchanged with a temporary container without elements, and finally the size of the container that needs to be shrunk is the same as the size of the capacity, which achieves the purpose of shrinking. The temporary object is destroyed when it goes out of scope, and the The resource is freed.

In addition, the shrink_to_fit() method can now be used to release the memory of the vector:

v1.clear();
//std::vector<int> tmp;
//tmp.swap(v1);
v1.shrink_to_fit();
std::cout << "size:" << v1.size() << " ,capacity:" << v1.capacity() << std::endl;

reference:

(1) The difference between the capacity and size attributes of vector in C++ STL

(2) The clear(), swap() and shrink_to_fit() methods of vector in c++

(3)

Guess you like

Origin blog.csdn.net/mars21/article/details/131614788