Description:
Double-ended queue that supports efficient insertion and deletion at both ends.
Resembles a vector but provides constant time complexity for insertion and deletion at both the beginning and the end.
Suitable for scenarios requiring fast insertion and deletion at both ends.
Can be used as a dynamic array with automatic resizing.
ALL THE POSSIBLE WAYS TO DECLARE, INITIALIZE AND PRINTING DEQUE:
#include <iostream>
#include <deque>
#include <string>
using namespace std;
int main() {
// Declaration of an empty deque of integers
deque<int> myDeque;
// Declaration of a deque of doubles
deque<double> doubleDeque;
// Declaration of a deque of strings
deque<string> stringDeque;
// Initializing deque with values using initializer list
deque<int> initializedDeque = {1, 2, 3, 4, 5};
// Initializing deque with a specific size and default value
deque<int> sizedDeque(5, 0); // Deque of size 5, all elements initialized to 0
// Dynamic initialization using push_back
deque<int> dynamicDeque;
dynamicDeque.push_back(10);
dynamicDeque.push_back(20);
dynamicDeque.push_back(30);
// Copying from another deque
deque<int> sourceDeque = {1, 2, 3, 4, 5};
deque<int> copiedDeque(sourceDeque); // Copy constructor
// Printing deque using for loop with index
cout << "Initialized Deque (index): ";
for (it i = 0; i < initializedDeque.size(); ++i) {
cout << initializedDeque[i] << " ";
}
cout << endl;
// Printing deque using for loop and deque.begin()
cout << "Sized Deque (deque.begin()): ";
for (auto it = sizedDeque.begin(); it != sizedDeque.end(); ++it) {
cout << *it << " ";
}
cout << endl;
// Printing deque using for loop and iterator
cout << "Dynamic Deque (iterator): ";
for (deque<int>::iterator it = dynamicDeque.begin(); it != dynamicDeque.end(); ++it) {
cout << *it << " ";
}
cout << endl;
// Printing deque using for loop and auto keyword
cout << "Copied Deque (auto): ";
for (auto element : copiedDeque) {
cout << element << " ";
}
cout << endl;
return 0;
}
OUTPUT:
Initialized Deque (index): 1 2 3 4 5
Sized Deque (deque.begin()): 0 0 0 0 0
Dynamic Deque (iterator): 10 20 30
Copied Deque (auto): 1 2 3 4 5
MEMBER FUNCTIONS:
At(g):
Works: Returns a reference to the element at position 'g'.
Syntax:
myDeque.at(g)
Example:
deque<int> myDeque = {1, 2, 3, 4, 5}; int elementAtPositionG = myDeque.at(2);
Front:
Works: Returns a reference to the first element.
Syntax:
myDeque.front()
Example:
deque<int> myDeque = {1, 2, 3, 4, 5}; int firstElement = myDeque.front();
Back:
Works: Returns a reference to the last element.
Syntax:
myDeque.back()
Example:
deque<int> myDeque = {1, 2, 3, 4, 5}; int lastElement = myDeque.back();
Data:
Works: Returns a direct pointer to the memory array used internally.
Syntax:
myDeque.data
()
Example:
deque<int> myDeque = {1, 2, 3, 4, 5}; int* dataPointer = myDeque.data();
Size:
Works: Returns the number of elements in the deque.
Syntax:
myDeque.size()
Example:
deque<int> myDeque = {1, 2, 3, 4, 5}; std::size_t dequeSize = myDeque.size();
Max Size:
Works: Returns the maximum number of elements that the deque can hold.
Syntax:
myDeque.max_size()
Example:
deque<int> myDeque; std::size_t maxDequeSize = myDeque.max_size();
Empty:
Works: Returns whether the deque is empty.
Syntax:
myDeque.empty()
Example:
deque<int> myDeque = {1, 2, 3, 4, 5}; bool isDequeEmpty = myDeque.empty();
Begin(), End(), Rbegin(), Rend():
Works: Iterator functions for the deque.
Syntax:
myDeque.begin()
,myDeque.end()
,myDeque.rbegin()
,myDeque.rend()
Example:
deque<int> myDeque = {1, 2, 3, 4, 5}; auto beginIterator = myDeque.begin(); auto endIterator = myDeque.end(); auto rbeginIterator = myDeque.rbegin(); auto rendIterator = myDeque.rend();
CODE 👍
#include <iostream>
#include <deque>
int main() {
// Creating a deque
std::deque<int> myDeque;
// Adding elements to the deque
for (int i = 1; i <= 5; ++i) {
myDeque.push_back(i * 10);
}
// Accessing elements using at()
std::cout << "Element at position 2: " << myDeque.at(1) << std::endl;
// Accessing the first and last element
std::cout << "First element: " << myDeque.front() << std::endl;
std::cout << "Last element: " << myDeque.back() << std::endl;
// Accessing elements using iterators (begin, end, rbegin, rend)
std::cout << "Using iterators: ";
for (auto it = myDeque.begin(); it != myDeque.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
// Accessing elements using reverse iterators
std::cout << "Using reverse iterators: ";
for (auto rit = myDeque.rbegin(); rit != myDeque.rend(); ++rit) {
std::cout << *rit << " ";
}
std::cout << std::endl;
// Accessing the underlying array using data()
int* dataArray = myDeque.data();
std::cout << "Memory array: ";
for (std::size_t i = 0; i < myDeque.size(); ++i) {
std::cout << dataArray[i] << " ";
}
std::cout << std::endl;
// Getting the size and max_size
std::cout << "Size of deque: " << myDeque.size() << std::endl;
std::cout << "Maximum size of deque: " << myDeque.max_size() << std::endl;
// Checking if the deque is empty
std::cout << "Is deque empty? " << (myDeque.empty() ? "Yes" : "No") << std::endl;
return 0;
}
OUTPUT:
Element at position 2: 20
First element: 10
Last element: 50
Using iterators: 10 20 30 40 50
Using reverse iterators: 50 40 30 20 10
Memory array: 10 20 30 40 50
Size of deque: 5
Maximum size of deque: 4611686018427387903
Is deque empty? No
MODIFIERS:
Assign():
Works: Assigns new values to deque elements.
Syntax:
myDeque.assign(first_iterator, last_iterator)
Example:
deque<int> myDeque; vector<int> newValues = {1, 2, 3}; myDeque.assign(newValues.begin(), newValues.end());
Push Back:
Works: Adds elements to the end of the deque.
Syntax:
myDeque.push_back(value)
Example:
deque<int> myDeque; myDeque.push_back(6);
Push Front:
Works: Adds elements to the beginning of the deque.
Syntax:
myDeque.push_front(value)
Example:
deque<int> myDeque; myDeque.push_front(3);
Pop Back:
Works: Removes elements from the end of the deque.
Syntax:
myDeque.pop_back()
Example:
deque<int> myDeque; myDeque.push_back(6); myDeque.pop_back();
Pop Front:
Works: Removes elements from the beginning of the deque.
Syntax:
myDeque.pop_front()
Example:
deque<int> myDeque; myDeque.push_back(3); myDeque.pop_front();
Insert:
Works: Inserts new elements at a specified position.
Syntax:
myDeque.insert(iterator_position, value)
Example:
deque<int> myDeque = {1, 2, 3, 4, 5}; myDeque.insert(myDeque.begin() + 2, 8);
Erase:
Works: Removes elements from a specified position or range.
Syntax:
myDeque.erase(position)
Example:
deque<int> myDeque = {1, 2, 3, 4, 5}; myDeque.erase(myDeque.begin() + 3);
Swap:
Works: Swaps contents with another deque.
Syntax:
myDeque.swap(otherDeque)
Example:
deque<int> myDeque1 = {1, 2, 3}; deque<int> myDeque2 = {4, 5, 6}; myDeque1.swap(myDeque2);
Clear:
Works: Removes all elements from the deque.
Syntax:
myDeque.clear()
Example:
deque<int> myDeque = {1, 2, 3}; myDeque.clear();
Emplace:
Works: Inserts a new element at a position.
Syntax:
myDeque.emplace(iterator_position, args)
Example:
deque<int> myDeque; myDeque.emplace(myDeque.begin() + 2, 10);
Emplace Back:
Works: Inserts a new element at the end.
Syntax:
myDeque.emplace_back(args)
Example:
deque<int> myDeque; myDeque.emplace_back(12);
Emplace Front:
Works: Inserts a new element at the beginning.
Syntax:
myDeque.emplace_front(args)
Example:
deque<int> myDeque; myDeque.emplace_front(5);
Code 👍
#include <iostream>
#include <deque>
int main() {
// Declare a deque
std::deque<int> myDeque;
// push_back(): Adds elements to the end
myDeque.push_back(1);
myDeque.push_back(2);
myDeque.push_back(3);
// push_front(): Adds elements to the beginning
myDeque.push_front(0);
// pop_back(): Removes elements from the end
myDeque.pop_back();
// pop_front(): Removes elements from the beginning
myDeque.pop_front();
// insert(): Inserts new elements at a specified position
myDeque.insert(myDeque.begin() + 1, 5);
// erase(): Removes elements from a specified position or range
myDeque.erase(myDeque.begin() + 1);
// swap(): Swaps contents with another deque
std::deque<int> anotherDeque = {7, 8, 9};
myDeque.swap(anotherDeque);
// clear(): Removes all elements
myDeque.clear();
// emplace(): Inserts a new element at a position
myDeque.emplace(myDeque.begin(), 10);
// emplace_back(): Inserts a new element at the end
myDeque.emplace_back(11);
// emplace_front(): Inserts a new element at the beginning
myDeque.emplace_front(9);
// Display the elements in the deque
for (const auto& element : myDeque) {
std::cout << element << " ";
}
return 0;
}
OUTPUT:
9,10,11
ALGORITHM:
Binary Search:
Works: Conducts a binary search on an ordered sequence.
Syntax:
binary_search(first_iterator, last_iterator, value)
Example:
deque<int> myDeque = {1, 2, 3, 4, 5}; bool isValuePresent = binary_search(myDeque.begin(), myDeque.end(), 7);
Equal Range:
Works: Finds a subrange of elements with a given value.
Syntax:
equal_range(first_iterator, last_iterator, value)
Example:
deque<int> myDeque = {1, 2, 3, 4, 5}; auto range = equal_range(myDeque.begin(), myDeque.end(), 5);
Inplace Merge:
Works: Merges two consecutive sorted sequences.
Syntax:
inplace_merge(first_iterator, middle_iterator, last_iterator)
Example:
deque<int> myDeque = {1, 3, 5, 2, 4, 6}; inplace_merge(myDeque.begin(), myDeque.begin() + 3, myDeque.end());
Lower Bound:
Works: Finds the first occurrence of or the position to insert a specified value.
Syntax:
lower_bound(first_iterator, last_iterator, value)
Example:
deque<int> myDeque = {1, 2, 3, 4, 5}; auto position = lower_bound(myDeque.begin(), myDeque.end(), 3);
Make Heap:
Works: Creates a max heap from a sequence.
Syntax:
make_heap(first_iterator, last_iterator)
Example:
deque<int> myDeque = {3, 1, 4, 1, 5, 9}; make_heap(myDeque.begin(), myDeque.end());
Merge:
Works: Merges two sorted sequences.
Syntax:
merge(first1_iterator, last1_iterator, first2_iterator, last2_iterator, output_iterator)
Example:
deque<int> deque1 = {1, 3, 5}; deque<int> deque2 = {2, 4, 6}; deque<int> mergedDeque; merge(deque1.begin(), deque1.end(), deque2.begin(), deque2.end(), back_inserter(mergedDeque));
Partition:
Works: Places elements matching a predicate in the first part.
Syntax:
partition(first_iterator, last_iterator, predicate)
Example:
deque<int> myDeque = {1, 2, 3, 4, 5}; partition(myDeque.begin(), myDeque.end(), [](int x){ return x % 2 == 0; });
Sort:
Works: Sorts a sequence.
Syntax:
sort(first_iterator, last_iterator)
Example:
deque<int> myDeque = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3}; sort(myDeque.begin(), myDeque.end());
Sort Heap:
Works: Converts a max heap into a sorted range.
Syntax:
sort_heap(first_iterator, last_iterator)
Example:
deque<int> myDeque = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3}; make_heap(myDeque.begin(), myDeque.end()); sort_heap(myDeque.begin(), myDeque.end());
Upper Bound:
Works: Finds the position after the last occurrence of a specified value.
Syntax:
upper_bound(first_iterator, last_iterator, value)
Example:
deque<int> myDeque = {1, 2, 3, 4, 5}; auto position = upper_bound(myDeque.begin(), myDeque.end(), 4);
Stable Sort:
Works: Sorts while maintaining the relative order of equal elements.
Syntax:
stable_sort(first_iterator, last_iterator)
Example:
deque<int> myDeque = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3}; stable_sort(myDeque.begin(), myDeque.end());
Max:
Works: Returns the greater of two values.
Syntax:
max(a, b)
Example:
int largerValue = max(5, 8);
Max Element:
Works: Finds the largest element in a range.
Syntax:
max_element(first_iterator, last_iterator)
Example:
deque<int> myDeque = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3}; auto maxElement = max_element(myDeque.begin(), myDeque.end());
Min:
Works: Returns the smaller of two values.
Syntax:
min(a, b)
Example:
int smallerValue = min(3, 6);
Min Element:
Works: Finds the smallest element in a range.
Syntax:
min_element(first_iterator, last_iterator)
Example:
deque<int> myDeque = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3}; auto minElement = min_element(myDeque.begin(), myDeque.end());
Accumulate:
Works: Accumulates the result of an operation on a sequence.
Syntax:
accumulate(first_iterator, last_iterator, initial_value)
Example:
deque<int> myDeque = {1, 2, 3, 4, 5}; int sum = accumulate(myDeque.begin(), myDeque.end(), 0);
CODE 👍
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;
int main() {
// Example deque
deque<int> myDeque = {4, 2, 7, 1, 5, 9, 3, 6, 8};
// Binary Search
sort(myDeque.begin(), myDeque.end());
bool binarySearchResult = binary_search(myDeque.begin(), myDeque.end(), 5);
cout << "Binary Search Result: " << binarySearchResult << endl;
// Equal Range
auto equalRange = equal_range(myDeque.begin(), myDeque.end(), 5);
cout << "Equal Range: [" << equalRange.first - myDeque.begin() << ", " << equalRange.second - myDeque.begin() << ")" << endl;
// Inplace Merge
inplace_merge(myDeque.begin(), myDeque.begin() + 5, myDeque.end());
cout << "Inplace Merge Result: ";
for (const auto& element : myDeque) {
cout << element << " ";
}
cout << endl;
// Lower Bound
auto lowerBound = lower_bound(myDeque.begin(), myDeque.end(), 5);
cout << "Lower Bound: " << lowerBound - myDeque.begin() << endl;
// Make Heap
make_heap(myDeque.begin(), myDeque.end());
cout << "Max Heap: ";
for (const auto& element : myDeque) {
cout << element << " ";
}
cout << endl;
// Merge
deque<int> secondDeque = {10, 12, 14};
merge(myDeque.begin(), myDeque.end(), secondDeque.begin(), secondDeque.end(), myDeque.begin());
cout << "Merged Result: ";
for (const auto& element : myDeque) {
cout << element << " ";
}
cout << endl;
// Partition
auto partitionPoint = partition(myDeque.begin(), myDeque.end(), [](int x) { return x % 2 == 0; });
cout << "Partitioned Result: ";
for (const auto& element : myDeque) {
cout << element << " ";
}
cout << endl;
// Sort
sort(myDeque.begin(), myDeque.end());
cout << "Sorted Result: ";
for (const auto& element : myDeque) {
cout << element << " ";
}
cout << endl;
// Sort Heap
sort_heap(myDeque.begin(), myDeque.end());
cout << "Sorted Heap: ";
for (const auto& element : myDeque) {
cout << element << " ";
}
cout << endl;
// Upper Bound
auto upperBound = upper_bound(myDeque.begin(), myDeque.end(), 5);
cout << "Upper Bound: " << upperBound - myDeque.begin() << endl;
// Stable Sort
deque<int> stableSortDeque = {4, 2, 7, 1, 5, 9, 3, 6, 8};
stable_sort(stableSortDeque.begin(), stableSortDeque.end());
cout << "Stable Sorted Result: ";
for (const auto& element : stableSortDeque) {
cout << element << " ";
}
cout << endl;
// Max
int maxResult = max(10, 5);
cout << "Max Result: " << maxResult << endl;
// Max Element
auto maxElement = max_element(myDeque.begin(), myDeque.end());
cout << "Max Element: " << *maxElement << endl;
// Min
int minResult = min(10, 5);
cout << "Min Result: " << minResult << endl;
// Min Element
auto minElement = min_element(myDeque.begin(), myDeque.end());
cout << "Min Element: " << *minElement << endl;
// Accumulate
int accumulateResult = accumulate(myDeque.begin(), myDeque.end(), 0);
cout << "Accumulate Result: " << accumulateResult << endl;
return 0;
}
OUTPUT:
Binary Search Result: 1
Equal Range: [4, 5)
Inplace Merge Result: 1 2 3 4 5 6 7 8 9
Lower Bound: 4
Max Heap: 9 8 7 6 5 2 3 4 1
Merged Result: 1 2 3 4 5 6 7 8 9 10 12 14
Partitioned Result: 2 4 6 8 1 3 7 9 5
Sorted Result: 1 2 3 4 5 6 7 8 9
Sorted Heap: 1 2 3 4 5 6 7 8 9
Upper Bound: 5
Stable Sorted Result: 1 2 3 4 5 6 7 8 9
Max Result: 10
Max Element: 9
Min Result: 5
Min Element: 1
Accumulate Result: 45
CONGRATULATION NOW YOU ARE THE MASTER OF DEQUE CONTAINER