How to Create Array of Arrays in C++
Last Updated :
12 May, 2024
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 arrays in C++.
Create an Array of Arrays in C++
To create an array of arrays also known as 2D arrays, you can follow the same approach you follow to create a normal one-dimensional array with some minor changes. Like in normal one-dimensional arrays, you define only the number of columns for the array here you will have to define an additional dimension row along with the number of columns. Here is the syntax to create an array of arrays in C++:
Syntax
data_type Arr [rows][columns];
where:
- data_type: denotes the type of data you want to store in the array.
- Arr: is the name of the array of arrays.
- rows: denotes the number of rows in your array of arrays.
- columns: denotes the number of columns in your array of arrays.
C++ Program to Create Array of Arrays
The following program demonstrates how to create array of arrays in C++
C++
// C++ Program to demonstrate how to create array of arrays
#include <iostream>
using namespace std;
int main()
{
// Declare and initialize a 2D array
int arr[3][3]
= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
// Print the elements of the array
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
cout << arr[i][j] << " ";
}
cout << endl;
}
return 0;
}
Time Complexity:O(N*M) where N denotes the number of rows and M denotes the number of columns.
Auxiliary Space:O(N*M)
Create Array of Arrays for std::array Container in C++
In C++, we have a wrapper container class for arrays named std::array. We can also create arrays of this type of array container f you are using the below syntax:
Syntax
std::array<std::array<dataType, COLS>, ROWS> matrix = {{ {x,y,z} .... }};
where:
- data_type: denotes the type of data you want to store in the array of arrays.
- Matrix: is the name of the array of arrays.
- ROWS: denotes the number of rows in your array of arrays.
- COLS: denotes the number of columns in your array of arrays.
C++ Program to Create std::Array of Arrays
The following program demonstrates how we can create array of arrays using std::arrays in C++:
C++
// C++ Program to demonstrate how we can create array of
// arrays using std::arrays
#include <array>
#include <iostream>
using namespace std;
// Declare the dimensions for the arrays of arrays
const int ROWS = 3;
const int COLS = 3;
int main()
{
// Initialize an array of arrays
array<array<int, COLS>, ROWS> matrix
= { { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } } };
// Print all elements
for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLS; ++j) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
Time Complexity:O(N*M) where N denotes the number of rows and M denotes the number of columns.
Auxiliary Space:O(N*M)
Similar Reads
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 Stack of Arrays in C++? In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::array is a sequence container that stores elements in contiguous memory. In this article, we will learn how to create a stack of an array in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; ar
2 min read
How to Create a Deque of Arrays in C++? 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,
2 min read
How to Create a Vector of Arrays in C++? In C++, an array is a collection of elements of a single type while vectors are dynamic arrays as they can change their size during the insertion and deletion of elements. In this article, we will learn how to create a vector of arrays in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; arr3
2 min read