Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy multidimensional array to vector and vice versa

Tags:

c++

arrays

vector

int dArray[1600][32];
vector < vector <int> > dVector;

n= 1600; k = 32
dVector.resize(n);
for(int i = 0 ; i < n ; ++i){
dVector[i].resize(k);
}

std::copy ( dArray, dArray + tmp_c, std::back_inserter (dVector));

How do i use the std::copy ( or any other function ) to copy an multi dimensional array to a vector and vice versa?

like image 209
Gambit King Avatar asked Oct 22 '22 21:10

Gambit King


1 Answers

You can't do it directly, but with an intermediate step. Depending on your requirements, something like this vector_wrapper might work for you

#include <vector>

template<typename T, int N> struct vector_wrapper {
    vector_wrapper(T (&a)[N]) {
        std::copy(a, a + N, std::back_inserter(v));
    }

    std::vector<T> v;
};

int dArray[1600][32];
std::vector<vector_wrapper<int, 32> > dVector;

int main(int argc, char **argv)
{
    std::copy(dArray, dArray + 1600, std::back_inserter(dVector));
    return 0;
}
like image 108
Olaf Dietsche Avatar answered Oct 27 '22 00:10

Olaf Dietsche