C++ Deque Library - push_back() Function



Description

The C++ function std::deque::push_back() inserts new element at the end of deque and increases size of deque by one.

Declaration

Following is the declaration for std::deque::push_back() function form std::deque header.

C++11

void push_back (value_type&& val);

Parameters

val − Value of element to be inserted into deque.

Return value

None

Exceptions

This member function never throws exception.

Time complexity

Constant i.e. O(1)

Example

The following example shows the usage of std::deque::push_back() function.

#include <iostream>
#include <deque>

using namespace std;

int main(void) {

   deque<int> d1 = {1, 2, 3, 4, 5};
   deque<int> d2;

   for (int i = 0; i < d1.size(); ++i)
      d2.push_back(move(d1[i]));

   cout << "Contents of deque are" << endl;

   for (int i = 0; i < d2.size(); ++i)
      cout << d2[i] << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

Contents of deque are
1
2
3
4
5
deque.htm
Advertisements