A 2D vector is a vector of vector. Like 2D arrays, we can declare and assign values to a 2D vector!
Assuming you are familiar with a normal vector in C++, with the help of an example we demonstrate how a 2D vector differs from a normal vector below:
In a 2D vector, every element is a vector.
1 2 3 4 5 6 7 8 9
Like Java’s jagged arrays, each element of a 2D vector can contain a different number of values.
1 2 4 5 6 7 8 9 10
Exercise Problem : Define the 2D vector with different sizes of column.
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 Output : 1 2 3 1 2 1
2D vectors are often treated as a matrix with “rows” and “columns” inside it. Under the hood they are actually elements of the 2D vector.
We first declare an integer variable named “row” and then an array named “column” which is going to hold the value of the size of each row.
After that we proceed to 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 initialize a 2D vector of “n” rows and “m” columns, with a value 0.
0 0 0 0 0 0 0 0 0 0 0 0
Yet Another Approach:
Suppose we want to create a 2D vector of “n” rows and “b” columns and input values.
1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8
We hope you that you leave this article with a better understanding of 2D vectors and are now confident enough to apply them on your own.
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Function Interposition in C with an example of user defined malloc()
- User Defined Literals in C++
- Multi-set for user defined data type
- User defined Data Types in C++
- 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++?
- How to implement user defined Shared Pointers in C++
- vector::empty() and vector::size() in C++ STL
- vector::front() and vector::back() in C++ STL
- vector::push_back() and vector::pop_back() in C++ STL
- vector::operator= and vector::operator[ ] in C++ STL
- vector::at() and vector::swap() in C++ STL
- vector::crend() & vector::crbegin() with example
- vector::begin() and vector::end() in C++ STL
- vector :: cbegin() and vector :: cend() in C++ STL
- How to flatten a Vector of Vectors or 2D Vector in C++
- C++ set for user define data type
- C++ map having key as a user define data type
- Sorting 2D Vector in C++ | Set 2 (In descending order by row and column)
- Common Subtleties in Vector STLs