Merge vector of vectors into a single vector

Humam Helfawi

I have vector of vectors of T:

std::vector<std::vector<T>> vector_of_vectors_of_T;

I want to merge all of them into single vector of T:

std::vector<T> vector_of_T;

I am currently using this method:

size_t total_size{ 0 };
for (auto const& items: vector_of_vectors_of_T){
    total_size += items.size();
}
vector_of_T.reserve(total_size);
for (auto const& items: vector_of_vectors_of_T){
    vector_of_T.insert(end(vector_of_T), begin(items), end(items));
}

Is there more straightforward method? Like a ready std function? If not, is there more efficient way to do it manually?

TemplateRex

It's a nice exercise to try and write up a generic join. The code below takes a nested container R1<R2<T> and returns a joined container R1<T>. Note that because of the allocator parameters in the Standard Library, this is a bit cumbersome. No attempt is being made to check for allocator compatibility etc.

Fortunately, there's action::join function in the upcoming range-v3 library by Eric Niebler, that is quite robust already and works today on Clang:

#include <range/v3/all.hpp>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>

// quick prototype
template<template<class, class...> class R1, template<class, class...> class R2, class T, class... A1, class... A2>
auto join(R1<R2<T, A2...>, A1...> const& outer)
{
    R1<T, A2...> joined;
    joined.reserve(std::accumulate(outer.begin(), outer.end(), std::size_t{}, [](auto size, auto const& inner) {
        return size + inner.size();
    }));
    for (auto const& inner : outer)
        joined.insert(joined.end(), inner.begin(), inner.end());
    return joined;
}

int main()
{
    std::vector<std::vector<int>> v = { { 1, 2 }, { 3, 4 } };

    // quick prototype
    std::vector<int> w = join(v);
    std::copy(w.begin(), w.end(), std::ostream_iterator<int>(std::cout, ",")); std::cout << "\n";

    // Eric Niebler's range-v3
    std::vector<int> u = ranges::action::join(v);
    std::copy(u.begin(), u.end(), std::ostream_iterator<int>(std::cout, ",")); std::cout << "\n";
}

Live Example

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Concatenate/Merge vectors in single vector in clojure

From Dev

Merge two vectors and get a list of vector in R

From Dev

Reducing a matrix of feature vectors to a single, meaningful vector

From Dev

Is it possible to combine GSL vectors into a single vector?

From Dev

How to take a vector of vectors contents into a single column vector

From Dev

Vector of vectors, reserve

From Dev

Would vector of vectors be contiguous?

From Dev

Sorting a Vector of Vectors

From Dev

Match a vector to a list of vectors

From Dev

Printing a vector of vectors

From Dev

Iterating through vector of vectors

From Dev

Concatenate a vector of vectors of strings

From Dev

array of vectors or vector of arrays?

From Dev

Creating a Vector of Vectors in Rust

From Dev

Implementation of vector of vectors

From Dev

sorting vector of pair of vectors

From Dev

C++ "vector of vectors"

From Dev

C++ pushing elements of a vector of vectors into a vector

From Dev

Splitting a vector into vector of vectors based on key

From Dev

push_back a vector of vectors into a vector

From Dev

Deleting a vector in vector of vectors C++

From Dev

Split a vector into vector of vectors in clojure instead of vector of lists

From Dev

Vector out of bounds when assign a vector to a vector of vectors

From Dev

Convert local Vectors to RDD[Vector]

From Dev

What are the Issues with a vector-of-vectors?

From Dev

Vector operations on Eigen Array of Vectors

From Dev

get matrix of vectors from a vector

From Dev

How to group a vector into a list of vectors?

From Dev

How to initialize a vector of vectors on a struct?