Open In App

deque::emplace_front() and deque::emplace_back() in C++ STL

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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

This function is used to insert a new element into the deque container, the new element is added to the beginning of the deque.

Syntax :

dequename.emplace_front(value)
Parameters :
The element to be inserted into the deque
is passed as the parameter.
Result :
The parameter is added to the
deque at the beginning.

Examples:

Input  : mydeque{1, 2, 3, 4, 5};
mydeque.emplace_front(6);
Output : mydeque = 6, 1, 2, 3, 4, 5

Input : mydeque{};
mydeque.emplace_front(4);
Output : mydeque = 4

Errors and Exceptions

1. It has a strong exception guarantee, therefore, no changes are made if an exception is thrown.

2. The parameter should be of the same type as that of the container, otherwise, an error is thrown. 

CPP




// INTEGER DEQUE EXAMPLE
// CPP program to illustrate
// Implementation of emplace_front() function
#include <deque>
#include <iostream>
using namespace std;
 
int main()
{
    deque<int> mydeque;
    mydeque.emplace_front(1);
    mydeque.emplace_front(2);
    mydeque.emplace_front(3);
    mydeque.emplace_front(4);
    mydeque.emplace_front(5);
    mydeque.emplace_front(6);
 
    // deque becomes 6, 5, 4, 3, 2, 1
 
    // printing the deque
    for (auto it = mydeque.begin();
         it != mydeque.end(); ++it)
        cout << ' ' << *it;
 
    return 0;
}


Output:

6 5 4 3 2 1

CPP




// STRING DEQUE EXAMPLE
// CPP program to illustrate
// Implementation of emplace_front() function
#include <deque>
#include <iostream>
#include <string>
using namespace std;
 
int main()
{
    deque<string> mydeque;
    mydeque.emplace_front("portal");
    mydeque.emplace_front("science");
    mydeque.emplace_front("computer");
    mydeque.emplace_front("a");
    mydeque.emplace_front("is");
    mydeque.emplace_front("GEEKSFORGEEKS");
 
    // deque becomes GEEKSFORGEEKS, is, a,
    // computer, science, portal
 
    // printing the deque
    for (auto it = mydeque.begin();
                  it != mydeque.end(); ++it)
        cout << ' ' << *it;
 
    return 0;
}


Output:

GEEKSFORGEEKS is a computer science portal

Time Complexity : O(1)

Application

Given an empty deque, add integers to it using emplace_front() function and then calculate its size without using size function().

Input  : 1, 2, 3, 4, 5, 6
Output : 6

Algorithm

1. Add elements to the deque using emplace_front() function

2. Check if the deque is empty, if not, increment the counter variable initialised as 0, and pop the back element.

3. Repeat this step until the deque becomes empty.

4. Print the final value of the variable. 

CPP




// CPP program to illustrate
// Application of emplace_front() function
#include <deque>
#include <iostream>
using namespace std;
 
int main()
{
    // variable declaration
    int count = 0;
 
    // deque declaration
    deque<int> mydeque;
 
    // adding elements to deque
    mydeque.emplace_front(1);
    mydeque.emplace_front(2);
    mydeque.emplace_front(3);
    mydeque.emplace_front(4);
    mydeque.emplace_front(5);
    mydeque.emplace_front(6);
 
    // counting elements in deque
    while (!mydeque.empty()) {
        count++;
        mydeque.pop_back();
    }
    cout << count;
    return 0;
}


Output :

6
deque::emplace_back()

This function is used to insert a new element into the deque container, the new element is added to the end of the deque.

Syntax :

dequename.emplace_back(value)
Parameters :
The element to be inserted into the deque
is passed as the parameter.
Result :
The parameter is added to the
deque at the end.

Examples:

Input  : mydeque{1, 2, 3, 4, 5};
mydeque.emplace_back(6);
Output : mydeque = 1, 2, 3, 4, 5, 6

Input : mydeque{};
mydeque.emplace_back(4);
Output : mydeque = 4

Errors and Exceptions

1. It has a strong exception guarantee, therefore, no changes are made if an exception is thrown.

2. Parameter should be of same type as that of the container, otherwise an error is thrown. 

CPP




// INTEGER DEQUE EXAMPLE
// CPP program to illustrate
// Implementation of emplace_back() function
#include <deque>
#include <iostream>
using namespace std;
 
int main()
{
    deque<int> mydeque;
    mydeque.emplace_back(1);
    mydeque.emplace_back(2);
    mydeque.emplace_back(3);
    mydeque.emplace_back(4);
    mydeque.emplace_back(5);
    mydeque.emplace_back(6);
    // deque becomes 1, 2, 3, 4, 5, 6
 
    // printing the deque
    for (auto it = mydeque.begin();
             it != mydeque.end(); ++it)
        cout << ' ' << *it;
 
    return 0;
}


Output:

1 2 3 4 5 6

CPP




// STRING DEQUE EXAMPLE
// CPP program to illustrate
// Implementation of emplace_back() function
#include <deque>
#include <iostream>
using namespace std;
 
int main()
{
    deque<string> mydeque;
    mydeque.emplace_back("Hi");
    mydeque.emplace_back("this");
    mydeque.emplace_back("is");
    mydeque.emplace_back("geeksforgeeks");
    // deque becomes Hi this is geeksforgeeks
 
    // printing the deque
    for (auto it = mydeque.begin();
                it != mydeque.end(); ++it)
        cout << ' ' << *it;
 
    return 0;
}


Output:

Hi this is geeksforgeeks

Time Complexity : O(1)

Application

Given an empty deque, add integers to it using emplace_front() function and then calculate the sum of the elements.

Input  : 4, 5, 9, 2, 6
Output : 26

Algorithm

1. Add elements to the deque using emplace_back() function

2. Check if the deque is empty, if not, add the back value to the sum variable initialised as 0, and pop the back element.

3. Repeat this step until the deque becomes empty.

4. Print the final value of the variable. 

CPP




// CPP program to illustrate
// Application of emplace_back() function
#include <deque>
#include <iostream>
using namespace std;
 
int main()
{
    // variable declaration
    int sum = 0;
 
    // deque declaration
    deque<int> mydeque;
 
    // adding elements to deque
    mydeque.emplace_back(4);
    mydeque.emplace_back(5);
    mydeque.emplace_back(9);
    mydeque.emplace_back(2);
    mydeque.emplace_back(6);
 
    // counting sum of elements in deque
    while (!mydeque.empty()) {
        sum = sum + mydeque.back();
        mydeque.pop_back();
    }
    cout << sum;
    return 0;
}


Output :

6

Time complexity: O(n), // n is the number of elements in the dequeue.
Auxiliary Space: O(n).

Let us see the differences in a tabular form -:

 

deque::emplace_front()  deque::emplace_back() 
1.

It is used to insert a new element at the beginning of the deque

It is used to insert a new element at the end of the deque

2.

Its syntax is -:

emplace_front (Args&&… args);

Its syntax is -:

emplace_back (Args&&… args);

3.

It does not have any return value.

It does not have any return value.

4.

Its complexity is constant.

Its complexity is constant.

5.

It is present in

<deque>

header file

It is present in

<deque>

header file



Last Updated : 27 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads