vector::pop_back() function with example in C++ STL

C++ STL vector::pop_back() function: Here, we are going to learn about the pop_back() function of vector header in C++ STL with example.
Submitted by IncludeHelp, on May 16, 2019

C++ vector::pop_back() function

vector::pop_back() is a library function of "vector" header, it is used to deletes an element from the end of the vector, it deletes the element from the back and returns void.

Note: To use vector, include <vector> header.

Syntax of vector::pop_back() function

    vector::pop_back();

Parameter(s): none – it accepts nothing.

Return value: none – In returns nothing.

Example:

    Input:
    vector<int> v1{10, 20, 30, 40, 50};
    
    //removing elemenets
    v1.pop_back();  //removes 50
    v1.pop_back();  //removes 40

    Output:
    //if we print the values
    v1: 10 20 30

C++ program to demonstrate example of vector::pop_back() function

//C++ STL program to demonstrate example of
//vector::pop_back() function

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    //vector declaration
    vector<int> v1{ 10, 20, 30, 40, 50 };

    //printing elements
    cout << "v1: ";
    for (int x : v1)
        cout << x << " ";
    cout << endl;

    //removing elements
    v1.pop_back();
    v1.pop_back();

    //printing elements
    cout << "After removing elements..." << endl;
    cout << "v1: ";
    for (int x : v1)
        cout << x << " ";
    cout << endl;

    return 0;
}

Output

v1: 10 20 30 40 50
After removing elements...
v1: 10 20 30

Reference: C++ vector::pop_back()


Related Tutorials




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.