vector pop_back() in C++

pop_back(): It removes the last element from the vector. 

This is a typical stack operation. It shrinks the vector by one.

Note that no data is returned, and if the last element's data is needed, it should be retrieved before pop_back() is called.

Parameters: No parameters required for this function.

Syntax:

vecName.pop_back()

For Example:

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

vec.pop_back() => It removes last element (i.e 5) from the vector.

So vector becomes ={1,2,3,4}

Approach

C++

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

int main()
{
    vector<intvec = {12345};

    vec.pop_back();

    for (int i = 0i < vec.size(); i++)
        cout << vec[i] << " ";
        

    return 0;
}


No comments:

Post a Comment