![]() |
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++: Syntaxdata_type Arr [rows][columns]; where:
C++ Program to Create Array of ArraysThe following program demonstrates how to create array of arrays in C++
Output 1 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. 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:
C++ Program to Create std::Array of ArraysThe following program demonstrates how we can create array of arrays using std::arrays in C++:
Output 1 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. |
Reffered: https://www.geeksforgeeks.org
Arrays |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |