Skip to content
Related Articles

Related Articles

deque::emplace_front() and deque::emplace_back() in C++ STL
  • Difficulty Level : Basic
  • Last Updated : 30 Sep, 2019

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.




// 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




// 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 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.




// 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




// 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 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

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :