vector capacity() in C++

capacity(): It returns the total number of elements that the vector can hold before needing to

allocate more memory.

Syntax:

vecName.capacity():

File: stl_vector.h

For Example:

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

vec.capacity() = > It returns 8.

Approach

C++

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

int main()
{
    vector<intvec;
    for (int i = 1i <= 5i++)
        vec.push_back(i);
    cout << vec.capacity() << "\n";

    return 0;
}


No comments:

Post a Comment