vector shrink_to_fit() in C++

shrink_to_fit(): It is a build-in function in STL. 

A non-binding request to reduce capacity() to size(). 

File: stl_vector.h

Parameter: No parameter required.

Syntax:

vecName.shrink_to_fit()

For Example :

vec ={1,2,3,4,5}

capacity  = 8, size = 5

vec.shrink_to_fit()

capacity = 5, size  = 5

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<intvec;
    for (int i = 0i < 5i++)
        vec.push_back(i + 1);

    cout << vec.capacity() << " " << vec.size() << "\n";

    vec.shrink_to_fit();

    cout << vec.capacity() << " " << vec.size() << "\n";
 
    return 0;
}


No comments:

Post a Comment