C++ Deque Library - push_front() Function


Advertisements

Description

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

Declaration

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

C++11

void push_front (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_front() 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_front(move(d1[i]));

   cout << "Contents of deque d2 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 d2 are
5
4
3
2
1
deque.htm
Advertisements