Vector series in actual C + +_ To_ fit()

Vector has been almost written, and it seems to be nearing the end. Everything from initialization to how to add elements to copy element has been involved, so it is time to talk about the memory release.
Yes, for the vector with a small amount of data, it is completely unnecessary to actively release it by itself, because it will have little impact on the efficiency of the program. However, when a large amount of data is stored in the vector and some operations are carried out on the data, such as deletion, it is very wise if we can actively release the memory.
So if We write here, it should make the difference between size and capacity clear.
Now introduce one method, STD: : vector: : clear ()
Removes all elements from the vector (which are destroyed), brigade the container with a size of 0.
see clear, mentioned in the English language is size = 0, rather than capacity. Write programs to verify some:

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    v.push_back(5);

    cout << "size:" << v.size() << endl;
    cout << "capacity:" << v.capacity() << endl;

    v.clear();
    cout << "after clear size:" << v.size() << endl;
    cout << "after clear capacity:" << v.capacity() << endl;
    return 0;
}
//Output
size:5
capacity:6
after clear size:0
after clear capacity:6

See, after clearing, the size changes to 0 and the capacity remains the same. Read the description of clear in English again.
A reallocation is not guaranteed to happen, and the vector capacity is not guaranteed to change due to calling this function. A typical alternative that forces a reallocation is to use swap:
vector().swap(x); // clear x reallocating

std::vector::swap
Exchanges the content of the container by the content of x, which is another vector object of the same type. Sizes may differ.
After the call to this member function, the elements in this container are those which were in x before the call, and the elements of x are those which were in this. All iterators, references and pointers remain valid for the swapped objects.
Notice that a non-member function exists with the same name, swap, overloading that algorithm with an optimization that behaves like this member function.
A direct look at the use of.

#include <iostream>
#include <vector>

int main()
{
    std::vector<int> foo;
    foo.push_back(1);
    foo.push_back(2);
    foo.push_back(3);
    foo.push_back(4);
    foo.push_back(5);

    std::vector<int> bar;  
    bar.push_back(1);
    bar.push_back(2);


    std::cout << "foo size:" << foo.size() << std::endl;
    std::cout << "foo capacity:" << foo.capacity() << std::endl;

    std::cout << "bar size:" << bar.size() << std::endl;
    std::cout << "bar capacity:" << bar.capacity() << std::endl;
    foo.swap(bar);

    std::cout << "after swap foo size:" << foo.size() << std::endl;
    std::cout << "after swap foo capacity:" << foo.capacity() << std::endl;

    std::cout << "after swap bar size:" << bar.size() << std::endl;
    std::cout << "after swap bar capacity:" << bar.capacity() << std::endl;

    return 0;
}
//Output:
foo size:5
foo capacity:6
bar size:2
bar capacity:2
after swap foo size:2
after swap foo capacity:2
after swap bar size:5
after swap bar capacity:6

See, after swap, not only the size has changed, but the capacity has also changed. So swap replace clear:

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    v.push_back(5);

    cout << "size:" << v.size() << endl;
    cout << "capacity:" << v.capacity() << endl;

    vector<int>().swap(v);
    cout << "after swap size:" << v.size() << endl;
    cout << "after swap capacity:" << v.capacity() << endl;
    return 0;
}
//output:
size:5
capacity:6
after swap size:0
after swap capacity:0

Remember that shrink_to_fit() on the previous blog is called Shrink_to_fit (), and if clear then calls Shrink_to_fit (), then that doesn’t work?

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    v.push_back(5);

    cout << "size:" << v.size() << endl;
    cout << "capacity:" << v.capacity() << endl;

    v.clear();
    v.shrink_to_fit();
    cout << "after swap size:" << v.size() << endl;
    cout << "after swap capacity:" << v.capacity() << endl;
    return 0;
}
//output:
size:5
capacity:6
after swap size:0
after swap capacity:0

Instead of assuming that swap replaces clear can release the vector’s memory correctly, C++11 launches the shrink_to_fit method, and then does the same.

Read More: