How to Create a Deque of Arrays in C++?
Last Updated :
27 Mar, 2024
In C++, a deque (double-ended queue) is a data structure that allows insertion and deletion at both ends whereas arrays are fixed-size collections of elements. In this article, we will learn how to create a deque of arrays in C++ STL.
Example:
Input:
myArray1 = {1, 4, 8, 9, 11}
myArray2 = {1, 2, 3, 5, 7}
Output:
myDeque: [ {1, 4, 8, 9, 11} {1, 2, 3, 5, 7} ]
Creating a Deque of Arrays in C++
To create a std::deque
of std::array
in C++, we can pass the type of the deque container to be of the type std::array
as a template parameter while declaration.
Syntax to Create a Deque of Arrays in C++
deque<array<dataType, size>> dequeName;
Here,
- dataType denotes the data type of elements stored in the array.
- size denotes the size of the array.
- dequeName is a name of deque of arrays.
Note: We cannot create a deque of Plain Old Arrays as they do not meet the criteria for elements of deque containers which says that the elements should be at least copy constructible and assignable.
C++ Program to Create a Deque of Arrays
The below example demonstrates how we can create a deque of arrays in C++.
C++
// C++ Program to illustrate how to create a deque of array
#include <array>
#include <deque>
#include <iostream>
using namespace std;
int main()
{
// Declaring a deque of arrays
deque<array<int, 3> > myDeque;
// Creating some arrays
array<int, 3> arr1 = { 1, 2, 3 };
array<int, 3> arr2 = { 4, 5, 6 };
array<int, 3> arr3 = { 7, 8, 9 };
// Pushing arrays into the deque
myDeque.push_back(arr1);
myDeque.push_back(arr2);
myDeque.push_back(arr3);
// Checking if the deque is empty
cout << "myDeque: " << endl;
int i = 1;
for (auto& ele : myDeque) {
cout << "Array" << i++ << ": ";
for (auto i : ele) {
cout << i << " ";
}
cout << endl;
}
return 0;
}
OutputmyDeque:
Array1: 1 2 3
Array2: 4 5 6
Array3: 7 8 9
Time Complexity: O(N*M), where N is the number of arrays and M is the average size of each array.
Auxilliary Space: O(N*M)
Similar Reads
How to Create Array of Arrays in C++ Arrays are basic C++ data structures that allow users to store the same data type in memory sequentially. To manage more complicated data structures, you may sometimes need to build an array of arrays, often called a 2D array or a matrix. In this article, we will learn how to create an array of arra
3 min read
How to Create a Set of Arrays in C++? In C++, the set container represents a collection of unique, sorted elements, and an array is a collection of items stored at contiguous memory locations. In this article, we will learn about how to create a set of arrays in C++. Set of Arrays in C++A set of arrays refers to a collection of arrays w
2 min read
How to Create a Map of Arrays in C++? In C++, the std::map is a container that stores elements in a key-value pair, whereas std::array is a sequence container that stores elements in contiguous memory. In this article, we will learn how to create a map of arrays in C++. Example: Input: arr1 = {1, 2, 3};arr2 = {4, 5, 6};arr3 = {7, 8, 9};
2 min read
How to Create a Deque of Sets in C++? In C++, a container called deque is a queue like container but allows for fast insertions and deletions at both ends. In this article, we will learn how to create a deque of sets in C++. Example: Input: mySet1 = {1, 4, 8, 9, 11} mySet2 = {1, 2, 3, 5, 7} Output: myDeque: [ {1, 4, 8, 9, 11}, {1, 2, 3,
2 min read
How to Create a Deque of Maps in C++? In C++, the Standard Template Library (STL) provides a container called deque that allows efficient insertion and deletion operations at both ends of the container. A map is an associative container that stores elements in key-value pairs.In this article, we will learn how to create a deque of maps
2 min read