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
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Vector of Vectors in C++ STL with Examples
- Difference between std::remove and vector::erase for vectors
- vector :: cbegin() and vector :: cend() in C++ STL
- vector::push_back() and vector::pop_back() in C++ STL
- vector::empty() and vector::size() in C++ STL
- vector::front() and vector::back() in C++ STL
- vector::crend() & vector::crbegin() with example
- vector::begin() and vector::end() in C++ STL
- vector::at() and vector::swap() in C++ STL
- Vector in C++ STL
- Using std::vector::reserve whenever possible
- vector : : resize() in C++ STL
- How to reverse a Vector using STL in C++?
- vector::emplace_back in C++ STL
- Sorting a vector in C++
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.