Home » C++ STL

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()






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.