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
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.
- 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::empty() and vector::size() in C++ STL
- vector::front() and vector::back() in C++ STL
- vector::crend() & vector::crbegin() with example
- vector::push_back() and vector::pop_back() in C++ STL
- vector::begin() and vector::end() in C++ STL
- vector::at() and vector::swap() in C++ STL
- Vector in C++ STL
- How does a vector work in C++?
- vector::emplace_back in C++ STL
- Using std::vector::reserve whenever possible
- Sorting a vector in C++
- How to reverse a Vector using STL in C++?
- vector :: assign() in C++ STL
- vector : : resize() in C++ STL
- How to convert a Vector to Set in C++
- Modifiers for Vector in C++ STL
- Deque vs Vector 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.