Open In App

How to Remove an Element from the End of a Deque in C++?

Last Updated : 27 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C++, deque is also called a double-ended queue where we can insert new elements at both the front and back of the container. In this article, we will learn to remove an element from the end of a deque in C++.

Example:

Input:
myDeque = {1, 2, 3, 4, 5}

Output:
Deque after removal: 1 2 3 4 

Removing an Element from the End of a Deque in C++

To remove an element from the end of a std::deque in C++, we can use the std::deque::pop_back() method, which removes the last element of the deque, thereby reducing its size by one.

Syntax of std::pop_back

dequeName.pop_back();

C++ Program to Remove an Element from the End of a Deque

The below example demonstrates how we can remove an element from the end of a deque in C++.

C++
// C++ Program to demonstrates how we can remove an element
// from the end of a deque
#include <deque>
#include <iostream>

using namespace std;

int main()
{

    // Creating Deque with numbers
    deque<int> myDeque = { 1, 2, 3, 4, 5 };

    // Printing Deque before removing end element
    cout << "Deque before removal: ";
    for (int num : myDeque) {
        cout << num << " ";
    }
    cout << endl;

    // Removing element from the end of the deque
    myDeque.pop_back();

    // Printing Deque after removing the end of the deque
    cout << "Deque after removal: ";
    for (int num : myDeque) {
        cout << num << " ";
    }
    cout << endl;

    return 0;
}

Output
Deque before removal: 1 2 3 4 5 
Deque after removal: 1 2 3 4 

Time Complexity: O(1)
Auxilliary Space: O(1)




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads