A 2D vector is a vector of vector. Like 2D arrays, we can declare and assign values to 2D matrix
1 2 3 4 5 6 7 8 9
Like Java’s jagged arrays, each row of 2D vector can contain different number of columns.
1 2 4 5 6 7 8 9 10
Exercise Problem : Define the 2D vector with different sizes of column input by user.
Input : Number of rows : 5 Number of columns in rows : 2 3 4 5 1 Output : 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 Input : Number of rows : 3 Number of columns in rows : 3 2 1 Input : 3 3 2 1 Output : 1 2 3 1 2 1
First we take the input of row and and take the input of column of every row.
Now the initialize the memory of every row by the size of column.
1 2 3 4 5 1 2 3 1 2 3 4 1 2 1
Suppose we want to initialise a 2D vector of N rows and M column, with a value 0.
0 0 0 0 0 0 0 0 0 0 0 0
Suppose we want to create a 2D vector of N rows and M column, and input values.
1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8
This article is contributed by Amit Verma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- User Defined Literals in C++
- User defined Data Types in C++
- Function Interposition in C with an example of user defined malloc()
- How to implement user defined Shared Pointers in C++
- Multi-set for user defined data type
- How to create an unordered_map of user defined class in C++?
- How to create an unordered_set of user defined class or struct in C++?
- vector::empty() and vector::size() in C++ STL
- vector :: cbegin() and vector :: cend() in C++ STL
- vector::front() and vector::back() in C++ STL
- vector::push_back() and vector::pop_back() in C++ STL
- vector::crend() & vector::crbegin() with example
- How to flatten a Vector of Vectors or 2D Vector in C++
- vector::begin() and vector::end() in C++ STL
- vector::at() and vector::swap() in C++ STL
- C++ map having key as a user define data type
- C++ set for user define data type
- Vector in C++ STL
- How does a vector work in C++?
- Sorting a vector in C++
Improved By : Blinkii