How to Create Array of Arrays in C++ Last Updated : 12 May, 2024 Comments Improve Suggest changes Like Article Like Report 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; } Output1 2 3 4 5 6 7 8 9 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: Syntaxstd::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; } Output1 2 3 4 5 6 7 8 9 Time Complexity:O(N*M) where N denotes the number of rows and M denotes the number of columns.Auxiliary Space:O(N*M) Comment More infoAdvertise with us Next Article How to Create Array of Arrays in C++ R ravinp1w6 Follow Improve Article Tags : C++ Programs C++ Arrays C++ Array Programs CPP Examples +1 More Practice Tags : CPPArrays 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 How to Create a Multimap of Arrays in C++? In C++, a multimap is similar to a map with the addition that multiple elements can have the same keys. Also, it is NOT required that the key-value and mapped value pair have to be unique in this case. In this article, we will learn how to create a multimap of arrays in C++ STL. Example Input: myArr 2 min read How to Create an Array of Structs in C++? In C++, a struct is a user-defined data type that allows us to combine data of different types and an array of structs is an array in which each element is of the struct type. In this article, we will learn how to create an array of structs in C++. Creating an Array of Structs in C++To create an arr 2 min read How to Concatenate Two Arrays in C++? In C++, arrays store a fixed number of elements of the same type in contiguous memory locations. In this article, we will learn how to concatenate two arrays in C++. Example: Input: myArray1={10, 30, 40} myArray2 ={20, 50, 60}Output: Concatenated Array: 10 30 40 20 50 60 Concatenate Two Arrays in On 2 min read How to Declare an Array in C++? In C++, an array is a collection of similar data types in which elements are stored in contiguous memory locations. In this article, we will learn how to declare an array in C++. Declaring an Array in C++In C++, we can declare an array by specifying the type of its elements, followed by the name of 2 min read How to Deallocate a 2D Array in C++? In C++, an array of arrays is called a 2D array, or two-dimensional array. Deallocating a 2D array means freeing the allocated memory to prevent any memory leaks, especially when working with dynamically allocated memory. In this article, we will learn how to deallocate a 2D array in C++. Deallocati 3 min read Like