English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Usage and example of vector pop_back()

C++ Vector (Container)

It deletes the last element and reduces the size of the vector (vector) by one.

Syntax

Vector (vector) v. Syntax is:

v.pop_back();

Parameters

It does not contain any parameters.

Return value

It does not return any value.

The following figure shows how the pop_back() function works:



The following figure shows how to use the pop_back() function to delete the last element of a vector (vector).

Example

Let's see a simple example.

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector<string> v{"welcome","to","www.oldtoolbag.com","tutorial"};
    cout<<"Initial string is :";
    for(int i=0; i<v.size(); i++{
        cout << v[i] << " \t";
    }
    cout<<'\n';
    cout << "After deleting the last string, the string becomes:\";
    v.pop_back();
    for(int i = 0; i < v.size(); i++{
        cout << v[i] << " \t";
    }
    return 0;
}

Output:

The initial string is: welcome to www.oldtoolbag.com tutorial 
After deleting the last string, the string becomes: welcome to www.oldtoolbag.com

In this example, the last string is deleted using the pop_back() function.

C++ Vector (Container)