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.双向队列是一种容器,可以在队列两端进行操作。和vector相似,但在插入和删除更有效率,不像vector,不一定需要连续存储。
The functions for deque are same as vector, with an addition of push and pop operations for both front and back.
#include <iostream>
#include <deque>
using namespace std;
void showdq(deque <int> g)
{
deque <int> :: iterator it;
for (it = g.begin(); it != g.end(); ++it)
cout << '\t' << *it;
cout << '\n';
}
int main()
{
deque <int> gquiz;
gquiz.push_back(10);
gquiz.push_front(20);
gquiz.push_back(30);
gquiz.push_front(15);
cout << "The deque gquiz is : ";
showdq(gquiz);
cout << "\ngquiz.size() : " << gquiz.size();
cout << "\ngquiz.max_size() : " << gquiz.max_size();
cout << "\ngquiz.at(2) : " << gquiz.at(2);
cout << "\ngquiz.front() : " << gquiz.front();
cout << "\ngquiz.back() : " << gquiz.back();
cout << "\ngquiz.pop_front() : ";
gquiz.pop_front();
showdq(gquiz);
cout << "\ngquiz.pop_back() : ";
gquiz.pop_back();
showdq(gquiz);
return 0;
}
The output of the above program is :
The deque gquiz is : 15 20 10 30
gquiz.size() : 4
gquiz.max_size() : 4611686018427387903
gquiz.at(2) : 10
gquiz.front() : 15
gquiz.back() : 30
gquiz.pop_front() : 20 10 30
gquiz.pop_back() : 20 10
Methods of Deque:
- deque insert() function in C++ STL: Returns an iterator that points to the first of the newly inserted elements.
- deque rbegin() function in C++ STL: Returns a reverse iterator which points to the last element of the deque (i.e., its reverse beginning).
- deque rend() function in C++ STL: Returns a reverse iterator which points to the position before the beginning of the deque (which is considered its reverse end).
- deque cbegin() in C++ STL: Returns an iterator pointing to the first element of the container.
- deque max_size() function in C++ STL: Returns the maximum number of elements that a deque container can hold.
- deque assign() function in C++ STL: Assign values to the same or different deque container.
- deque resize() function in C++ STL: Function which changes the size of the deque.
- deque::push_front() in C++ STL: This function is used to push elements into a deque from the front.
- deque::push_back() in C++ STL: This function is used to push elements into a deque from the back.
- deque::pop_front() and deque::pop_back() in C++ STL: pop_front() function is used to pop or remove elements from a deque from the front. pop_back() function is used to pop or remove elements from a deque from the back.
- deque::front() and deque::back() in C++ STL: front() function is used to reference the first element of the deque container. back() function is used to reference the last element of the deque container.
- deque::clear() and deque::erase() in C++ STL: clear() function is used to remove all the elements of the deque container, thus making its size 0. erase() function is used to remove elements from a container from the specified position or range.
- deque::empty() and deque::size() in C++ STL: empty() function is used to check if the deque container is empty or not. size() function is used to return the size of the deque container or the number of elements in the deque container.
- deque::operator= and deque::operator[] in C++ STL:
operator= operator is used to assign new contents to the container by replacing the existing contents. operator[] operator is used to reference the element present at position given inside the operator. - deque::at() and deque::swap() in C++ STL: at() function is used reference the element present at the position given as the parameter to the function. swap() function is used to swap the contents of one deque with another deque of same type and size.
- deque::begin() and deque::end in C++ STL: begin() function is used to return an iterator pointing to the first element of the deque container. end() function is used to return an iterator pointing to the last element of the deque container.
- deque::emplace_front() and deque::emplace_back() in C++ STL: emplace_front()function is used to insert a new element into the deque container, the new element is added to the beginning of the deque. emplace_back() function is used to insert a new element into the deque container, the new element is added to the end of the deque.
deque::front() and deque::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::front()
This function is used to reference the first element of the deque container. This function can be used to fetch the first element of a deque. front获得deque容器的第一个元素
Syntax :
dequename.front()
Parameters :
No value is needed to pass as the parameter.
Returns :
Direct reference to the first element of the deque container.
Examples:
Input : mydeque = 1, 2, 3
mydeque.front();
Output : 1
Input : mydeque = 3, 4, 1, 7, 3
mydeque.front();
Output : 3
Errors and Exceptions
- If the deque container is empty, it causes undefined behaviour
- It has a no exception throw guarantee if the deque is not empty
// CPP program to illustrate
// Implementation of front() function
#include <deque>
#include <iostream>
using namespace std;
int main()
{
deque<int> mydeque;
mydeque.push_back(3);
mydeque.push_back(4);
mydeque.push_back(1);
mydeque.push_back(7);
mydeque.push_back(3);
// Queue becomes 3, 4, 1, 7, 3
cout << mydeque.front();
return 0;
}
Output:
3
deque::back()
This function is used to reference the last element of the deque container. This function can be used to fetch the first element from the back of a deque. back获得deque容器的第一个元素
Syntax :
dequename.back()
Parameters :
No value is needed to pass as the parameter.
Returns :
Direct reference to the last element of the deque container.
Examples:
Input : mydeque = 1, 2, 3
mydeque.back();
Output : 3
Input : mydeque = 3, 4, 1, 7, 3
mydeque.back();
Output : 3
Errors and Exceptions
- If the deque container is empty, it causes undefined behaviour
- It has a no exception throw guarantee if the deque is not empty
// CPP program to illustrate
// Implementation of back() function
#include <deque>
#include <iostream>
using namespace std;
int main()
{
deque<int> mydeque;
mydeque.push_back(3);
mydeque.push_back(4);
mydeque.push_back(1);
mydeque.push_back(7);
mydeque.push_back(3);
// Queue becomes 3, 4, 1, 7, 3
cout << mydeque.back();
return 0;
}
Output:
3
Application :
Given an empty deque of integers, add numbers to the deque, then print the difference between the first and the last element.
Input : 1, 2, 3, 4, 5, 6, 7, 8
Output : 7
Explanation - Last element = 8, First element = 1, Difference = 7
Algorithm
1. Add numbers to the deque using push_back() function
2. Compare the first and the last element.
3. If first element is larger, subtract last element from it and print it.
4. Else subtract first element from the last element and print it.
// CPP program to illustrate
// application Of front() and back() function
#include <deque>
#include <iostream>
using namespace std;
int main()
{
deque<int> mydeque;
mydeque.push_back(8);
mydeque.push_back(7);
mydeque.push_back(6);
mydeque.push_back(5);
mydeque.push_back(4);
mydeque.push_back(3);
mydeque.push_back(2);
mydeque.push_back(1);
// deque becomes 1, 2, 3, 4, 5, 6, 7, 8
if (mydeque.front() > mydeque.back()) {
cout << mydeque.front() - mydeque.back();
}
else if (mydeque.front() < mydeque.back()) {
cout << mydeque.back() - mydeque.front();
}
else
cout << "0";
}
Output:
7
deque::empty() and deque::size() 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::empty()
empty() function is used to check if the deque container is empty or not. 判断队列是否为空。
Syntax :
dequename.empty()
Parameters :
No parameters are passed.
Returns :
True, if deque is empty
False, Otherwise
Examples:
Input : mydeque
mydeque.empty();
Output : True
Input : mydeque = 1, 2, 3
mydeque.empty();
Output : False
Errors and Exceptions
1. Shows error if parameter is passed
2. Shows no exception throw guarantee.
// CPP program to illustrate
// Implementation of empty() function
#include <deque>
#include <iostream>
using namespace std;
int main()
{
deque<int> mydeque;
mydeque.push_front(1);
// Deque becomes 1
if (mydeque.empty()) {
cout << "True";
}
else {
cout << "False";
}
return 0;
}
Output:
False
Application :
Given a deque of integers, find the sum of the all the integers.
Input : 1, 8, 3, 6, 2
Output: 20
Algorithm
1. Check if the deque is empty, if not add the front element to a variable initialised as 0, and pop the front element.
2. Repeat this step until the deque is empty.
3. Print the final value of the variable.
// CPP program to illustrate
// Application of empty() function
#include <deque>
#include <iostream>
using namespace std;
int main()
{
int sum = 0;
deque<int> mydeque;
mydeque.push_back(1);
mydeque.push_back(8);
mydeque.push_back(3);
mydeque.push_back(6);
mydeque.push_back(2);
// deque becomes 1, 8, 3, 6, 2
while (!mydeque.empty()) {
sum = sum + mydeque.front();
mydeque.pop_front();
}
cout << sum;
return 0;
}
Output:
20
deque::size()
size() function is used to return the size of the deque container or the number of elements in the deque container.获得队列的元素个数。
Syntax :
dequename.size()
Parameters :
No parameters are passed.
Returns :
Number of elements in the container.
Examples:
Input : mydeque = 0, 1, 2
mydeque.size();
Output : 3
Input : mydeque = 0, 1, 2, 3, 4, 5
mydeque.size();
Output : 6
Errors and Exceptions
1. Shows error if a parameter is passed.
2. Shows no exception throw guarantee.
// CPP program to illustrate
// Implementation of size() function
#include <deque>
#include <iostream>
using namespace std;
int main()
{
int sum = 0;
deque<int> mydeque;
mydeque.push_back(1);
mydeque.push_back(8);
mydeque.push_back(3);
mydeque.push_back(6);
mydeque.push_back(2);
// Deque becomes 1, 8, 3, 6, 2
cout << mydeque.size();
return 0;
}
Output:
5
Application :
Given a deque of integers, find the sum of the all the integers.
Input : 1, 8, 3, 6, 2
Output: 20
Algorithm
1. Check if the size of the deque is zero, if not add the front element to a variable initialised as 0, and pop the front element.
2. Repeat this step until the deque size becomes 0.
3. Print the final value of the variable.
// CPP program to illustrate
// Application of size() function
#include <deque>
#include <iostream>
using namespace std;
int main()
{
int sum = 0;
deque<int> mydeque;
mydeque.push_back(1);
mydeque.push_back(8);
mydeque.push_back(3);
mydeque.push_back(6);
mydeque.push_back(2);
// Deque becomes 1, 8, 3, 6, 2
while (mydeque.size() > 0) {
sum = sum + mydeque.front();
mydeque.pop_front();
}
cout << sum;
return 0;
}
Output:
20
deque::clear() and deque::erase() 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::clear()
clear() function is used to remove all the elements of the deque container, thus making its size 0. 删除队列中所有元素,大小变为0.
Syntax :
dequename.clear()
Parameters :
No parameters are passed.
Result :
All the elements of the deque are
removed ( or destroyed )
Examples:
Input : mydeque = {1, 2, 3, 4, 5}
mydeque.clear();
Output : mydeque = {}
Input : mydeque = {}
mydeque.clear();
Output : mydeque = {}
Errors and Exceptions
1. It has a no exception throw guarantee.
2. Shows error when a parameter is passed.
// CPP program to illustrate
// Implementation of clear() function
#include <deque>
#include <iostream>
using namespace std;
int main()
{
deque<int> mydeque{ 1, 2, 3, 4, 5 };
mydeque.clear();
// Deque becomes empty
// Printing the deque
for (auto it = mydeque.begin(); it != mydeque.end(); ++it)
cout << ' ' << *it;
return 0;
}
Output:
No Output
deque::erase()
erase() function is used to remove elements from a container from the specified position or range.
Syntax :
1. dequename.erase(position)
2. dequename.erase(startingposition, endingposition)
Parameters :
Position of the element to be removed in the form of iterator.
or the range specified using start and end iterator.
Result :
Elements are removed from the specified
position of the container.
Examples:
Input : mydeque{1, 2, 3, 4, 5}, iterator= 2
mydeque.erase(iterator);
Output : 1, 2, 4, 5
Input : mydeque{1, 2, 3, 4, 5, 6, 7, 8}, iterator1= 3, iterator2= 6
mydeque.erase(iterator1, iterator2);
Output : 1, 2, 3, 8
Errors and Exceptions
1. It has a no exception throw guarantee, if the position is valid.
2. Shows undefined behaviour otherwise.
Removing element from particular position
// CPP program to illustrate
// Implementation of erase() function
#include <deque>
#include <iostream>
using namespace std;
int main()
{
deque<int> mydeque{ 1, 2, 3, 4, 5 };
deque<int>::iterator it;
it = mydeque.begin();
mydeque.erase(it);
// Printing the deque
for (auto it = mydeque.begin(); it != mydeque.end(); ++it)
cout << ' ' << *it;
return 0;
}
Output:
2 3 4 5
Removing elements within a range
// CPP program to illustrate
// Implementation of erase() function
#include <deque>
#include <iostream>
using namespace std;
int main()
{
deque<int> mydeque{ 1, 2, 3, 4, 5 };
deque<int>::iterator it1, it2;
it1 = mydeque.begin();
it2 = mydeque.end();
it2--;
it2--;
mydeque.erase(it1, it2);
// Printing the deque
for (auto it = mydeque.begin(); it != mydeque.end(); ++it)
cout << ' ' << *it;
return 0;
}
Output:
4 5
Application
Given a list of integers, remove all the even elements from the deque and print the deque.
Input :1, 2, 3, 4, 5, 6, 7, 8, 9
Output :1 3 5 7 9
Explanation - 2, 4, 6 and 8 which are even are erased from the deque
Algorithm
1. Run a loop till the size of the deque.
2. Check if the element at each position is divisible by 2, if yes, remove the element and decrement iterator.
3. Print the final deque.
// CPP program to illustrate
// Application of erase() function
#include <deque>
#include <iostream>
using namespace std;
int main()
{
deque<int> mydeque{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for (auto i = mydeque.begin(); i != mydeque.end(); ++i) {
if (*i % 2 == 0) {
mydeque.erase(i);
i--;
}
}
// Printing the deque
for (auto it = mydeque.begin(); it != mydeque.end(); ++it)
cout << ' ' << *it;
return 0;
}
utput:
1 3 5 7 9
clear() VS erase() . When to use what?
clear() removes all the elements from a deque container, thus making its size 0. All the elements of the deque are removed using clear() function.
erase() function on the other hand, is used to remove specific elements from the container or a range of elements from the container, thus reducing its size by the number of elements removed.
deque::pop_front() and deque::pop_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::pop_front()
pop_front() function is used to pop or remove elements from a deque from the front. The value is removed from the deque from the beginning, and the container size is decreased by 1.在队头删除元素,大小降为1.
Syntax :
dequename.pop_front()
Parameters :
No value is needed to pass as the parameter.
Result :
Removes the value present at the front
of the given deque named as dequename
Examples:
Input : mydeque = 1, 2, 3
mydeque.pop_front();
Output : 2, 3
Input : mydeque = 3, 4, 1, 7, 3
mydeque.pop_front();
Output : 4, 1, 7, 3
Errors and Exceptions
- No-Throw-Guarantee – if an exception is thrown, there are no changes in the container
- If the deque is empty, it shows undefined behaviour.
// CPP program to illustrate
// pop_front() function
#include <iostream>
#include <deque>
using namespace std;
int main()
{
deque<int> mydeque;
mydeque.push_front(3);
mydeque.push_front(2);
mydeque.push_front(1);
//Deque becomes 1, 2, 3
mydeque.pop_front();
//Deque becomes 2, 3
for (auto it = mydeque.begin(); it != mydeque.end(); ++it)
cout << ' ' << *it;
}
Output:
2 3
Application : Input an empty deque with the following numbers and order using push_front() function and print the reverse of the deque.
Input : 1, 2, 3, 4, 5, 6, 7, 8
Output: 8, 7, 6, 5, 4, 3, 2, 1
// CPP program to illustrate
// application Of pop_front() function
#include <iostream>
#include <deque>
using namespace std;
int main()
{
deque<int> mydeque{}, newdeque{};
mydeque.push_front(8);
mydeque.push_front(7);
mydeque.push_front(6);
mydeque.push_front(5);
mydeque.push_front(4);
mydeque.push_front(3);
mydeque.push_front(2);
mydeque.push_front(1);
//Deque becomes 1, 2, 3, 4, 5, 6, 7, 8
while (!mydeque.empty()) {
newdeque.push_front(mydeque.front());
mydeque.pop_front();
}
for (auto it = newdeque.begin(); it != newdeque.end(); ++it)
cout << ' ' << *it;
}
Output:
8 7 6 5 4 3 2 1
deque::pop_back()
pop_back() function is used to pop or remove elements from a deque from the back. The value is removed from the deque from the end, and the container size is decreased by 1.在队尾删除元素,大小降为1.
Syntax :
dequename.pop_back()
Parameters :
No value is needed to pass as the parameter.
Result :
Removes the value present at the end or back
of the given deque named as dequename
Examples:
Input : mydeque = 1, 2, 3
mydeque.pop_back();
Output : 1, 2
Input : mydeque = 3, 4, 1, 7, 3
mydeque.pop_back();
Output : 3, 4, 1, 7
Errors and Exceptions
- No-Throw-Guarantee – if an exception is thrown, there are no changes in the container
- If the deque is empty, it shows undefined behaviour.
// CPP program to illustrate
// pop_back() function
#include <iostream>
#include <deque>
using namespace std;
int main()
{
deque<int> mydeque;
mydeque.push_front(5);
mydeque.push_front(4);
mydeque.push_front(3);
mydeque.push_front(2);
mydeque.push_front(1);
//Deque becomes 1, 2, 3, 4, 5
mydeque.pop_back();
//Deque becomes 1, 2, 3, 4
for (auto it = mydeque.begin(); it != mydeque.end(); ++it)
cout << ' ' << *it;
}
Output:
1 2 3 4
Application :
Input an empty deque with the following numbers and order using push_front() function and print the reverse of the deque.
Input : 1, 20, 39, 43, 57, 64, 73, 82
Output : 82, 73, 64, 57, 43, 39, 20, 1
// CPP program to illustrate
// application Of pop_back() function
#include <iostream>
#include <deque>
using namespace std;
int main()
{
deque<int> mydeque, newdeque;
mydeque.push_front(82);
mydeque.push_front(73);
mydeque.push_front(64);
mydeque.push_front(57);
mydeque.push_front(43);
mydeque.push_front(39);
mydeque.push_front(20);
mydeque.push_front(1);
//Deque becomes 1, 20, 39, 43, 57, 64, 73, 82
while (!mydeque.empty()) {
newdeque.push_back(mydeque.back());
mydeque.pop_back();
}
for (auto it = newdeque.begin(); it != newdeque.end(); ++it)
cout << ' ' << *it;
}
Output:
82 73 64 57 43 39 20 1
deque::emplace_front() and deque::emplace_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::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 EXMAPLE
// 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
deque::begin() and deque::end 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::begin()
begin() function is used to return an iterator pointing to the first element of the deque container. begin() function returns a bidirectional iterator to the first element of the container.
Syntax :
dequename.begin()
Parameters :
No parameters are passed.
Returns :
This function returns a bidirectional
iterator pointing to the first element.
Examples:
Input : mydeque{1, 2, 3, 4, 5};
mydeque.begin();
Output : returns an iterator to the element 1
Input : mydeque{8, 7};
mydeque.begin();
Output : returns an iterator to the element 8
Errors and Exceptions
1. It has a no exception throw guarantee.
2. Shows error when a parameter is passed.
// CPP program to illustrate
// Implementation of begin() function
#include <deque>
#include <iostream>
using namespace std;
int main()
{
// declaration of deque container
deque<int> mydeque{ 1, 2, 3, 4, 5 };
// using begin() to print deque
for (auto it = mydeque.begin(); it != mydeque.end(); ++it)
cout << ' ' << *it;
return 0;
}
Output:
1 2 3 4 5
Time Complexity : O(1)
deque::end()
end() function is used to return an iterator pointing to the last element of the deque container. end() function returns a bidirectional iterator to the last element of the container.
Note : The last element of any container is considered as the theoretical element next to the last value stored in the container.
Syntax :
dequename.end()
Parameters :
No parameters are passed.
Returns :
This function returns a bidirectional
iterator pointing to the last element.
Examples:
Input : mydeque{1, 2, 3, 4, 5};
mydeque.end();
Output : returns an iterator to the element next to the element 5
Input : mydeque{8, 7};
mydeque.end();
Output : returns an iterator to the element next to the element 7
Errors and Exceptions
1. It has a no exception throw guarantee.
2. Shows error when a parameter is passed.
// CPP program to illustrate
// Implementation of end() function
#include <deque>
#include <iostream>
using namespace std;
int main()
{
// declaration of deque container
deque<int> mydeque{ 1, 2, 3, 4, 5 };
// using end() to print deque
for (auto it = mydeque.begin(); it != mydeque.end(); ++it)
cout << ' ' << *it;
return 0;
}
Output:
1 2 3 4 5
Time Complexity : O(1)