Given a Vector of Vectors (2D vector), the task is to flatten this 2d vector.
Input: vector = [[1, 2, 3, 4], [5, 6], [7, 8]]
Output: 1 2 3 4 5 6 7 8
Input: vector = [[1, 2], , [4, 5, 6, 8]]
Output: 1 2 3 4 5 6 8
- 2D Vector can be flattened using iterators.
- Store starting and ending iterator of every vector in two arrays, iStart & iEnd respectively.
- Create a hasNext() method to check if it has the vector has next element or not.
- Print the current element, if hasNext() yields true
Below is the implementation of the above approach:
1 2 3 4 5 6 7 8 9 10
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.
- Difference between std::remove and vector::erase for vectors
- Vector of Vectors in C++ STL with Examples
- Flatten a binary tree into linked list
- Quickly check if two STL vectors contain same elements or not
- Working with Array and Vectors using STL in C++
- How to join two Vectors using STL in C++?
- Cyclic Iterator for K variable length vectors
- Map of Vectors in C++ STL with Examples
- Array of Vectors in C++ STL
- Priority Queue of Vectors in C++ STL with Examples
- Set of Vectors in C++ STL with Examples
- Implementation of file allocation methods using vectors
- Left rotation of an array using vectors in C++
- 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::crend() & vector::crbegin() with example
- vector :: cbegin() and vector :: cend() in C++ STL
- vector::empty() and vector::size() in C++ STL
- vector::begin() and vector::end() in C++ STL
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.