# How to flatten a Vector of Vectors or 2D Vector in C++

Given a Vector of Vectors (2D vector), the task is to flatten this 2d vector.

Examples:

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

Algorithm:

1. 2D Vector can be flattened using iterators.
2. Store starting and ending iterator of every vector in two arrays, iStart & iEnd respectively.
3. Create a hasNext() method to check if it has the vector has next element or not.
4. Print the current element, if hasNext() yields true

Below is the implementation of the above approach:

 `// C++ program to flatten a ` `// Vector of Vectors or 2D Vector ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Class to flatten the 2d vector ` `class` `FlattenVector { ` ` `  `public``: ` `    ``int` `n; ` ` `  `    ``vector::iterator> iStart; ` `    ``vector::iterator> iEnd; ` `    ``int` `currIndex; ` ` `  `    ``// Store ending and starting iterators. ` `    ``FlattenVector(vector >& v) ` `    ``{ ` ` `  `        ``// Get the number ` `        ``// of rows in 2d vector ` `        ``n = v.size(); ` `        ``currIndex = 0; ` `        ``iStart.resize(n); ` `        ``iEnd.resize(n); ` ` `  `        ``for` `(``int` `i = 0; i < n; i++) { ` `            ``iStart[i] = v[i].begin(); ` `            ``iEnd[i] = v[i].end(); ` `        ``} ` `    ``} ` ` `  `    ``// Returns true if any element is left. ` `    ``bool` `hasNext() ` `    ``{ ` `        ``for` `(``int` `i = 0; i < n; i++) { ` `            ``if` `(iStart[i] != iEnd[i]) ` `                ``return` `true``; ` `        ``} ` `        ``return` `false``; ` `    ``} ` ` `  `    ``int` `next() ` `    ``{ ` `        ``// Vector at currIndex is printed, ` `        ``// increment currIndex. ` `        ``if` `(iStart[currIndex] ` `            ``== iEnd[currIndex]) { ` `            ``currIndex++; ` `            ``return` `next(); ` `        ``} ` ` `  `        ``// Increment iterator ` `        ``// and return the value. ` `        ``else` `            ``return` `*iStart[currIndex]++; ` `    ``} ` `}; ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``vector > ` `        ``v{ { 1, 2 }, ` `           ``{ 3 }, ` `           ``{ 4, 5, 6 }, ` `           ``{ 7, 8, 9, 10 } }; ` `    ``FlattenVector iter(v); ` ` `  `    ``while` `(iter.hasNext()) ` `        ``cout << iter.next() << ``" "``; ` ` `  `    ``return` `0; ` `} `

Output:

```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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.