deque::push_back() in C++ STL

Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed.

deque::push_back()

push_back() function is used to push elements into a deque from the back. The new value is inserted into the deque at the end, before the current last element and the container size is increased by 1.

Syntax :

dequename.push_back(value)
Parameters :
The value to be added in the back is 
passed as the parameter
Result :
Adds the value mentioned as the parameter 
to the back of the deque named as dequename

Examples:

Input : deque{1, 2, 3, 4, 5};
        deque.push_back(6);
Output : 1, 2, 3, 4, 5, 6

Input : deque{5, 4, 3, 2, 1};
        deque.push_back(6);
Output : 5, 4, 3, 2, 1, 6

Errors and Exceptions

1. Strong exception guarantee – if an exception is thrown, there are no changes in the container.
2. If the value passed as argument is not supported by the deque, it shows undefined behaviour.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate
// push_back() function
#include <iostream>
#include <deque>
using namespace std;
  
int main()
{
    deque<int> mydeque{ 1, 2, 3, 4, 5 };
    mydeque.push_back(6);
  
    // deque becomes 1, 2, 3, 4, 5, 6
  
    for (auto it = mydeque.begin(); 
              it != mydeque.end(); ++it)
        cout << ' ' << *it;
}

chevron_right


Output:

1 2 3 4 5 6

Time Complexity : O(1)

Application
Given an empty deque, add integers to it using push_back() function and then calculate sum of all its elements.

Input  : 11, 2, 5, 3, 7, 1
Output : 29

Algorithm
1. Add elements to the deque using push_back() function.
2. Check if the size of the deque is 0, if not, add the front element to the sum variable initialised as 0, and pop the front element.
3. Repeat this step until the size of the deque becomes 0.
4. Print the final value of the variable.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate
// Application of push_back() function
#include <iostream>
#include <deque>
using namespace std;
  
int main()
{
    int sum = 0;
    deque<int> mydeque;
    mydeque.push_back(11);
    mydeque.push_back(2);
    mydeque.push_back(5);
    mydeque.push_back(3);
    mydeque.push_back(7);
    mydeque.push_back(1);
    while (!mydeque.empty()) {
        sum = sum + mydeque.front();
        mydeque.pop_front();
    }
    cout << sum;
    return 0;
}

chevron_right


Output:

29


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : akshaydharsey



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.