Gambit King
Gambit King

Reputation: 483

Copy multidimensional array to vector and vice versa

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?

Upvotes: 5

Views: 5031

Answers (3)

Olaf Dietsche
Olaf Dietsche

Reputation: 74018

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;
}

Upvotes: 2

Yuushi
Yuushi

Reputation: 26040

You'll basically need to write your own kind of output iterator. It's a bit ugly, but something like this should do the trick:

#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>

template <typename ContainerOfContainer, typename Container, std::size_t n>
struct nested_back_inserter : public std::iterator<std::output_iterator_tag, void, 
                                                   void, void, void>
{
   std::size_t k;
   std::size_t current_;
   ContainerOfContainer* container_;

   explicit nested_back_inserter(ContainerOfContainer& cont)
     : k(0), current_(0), container_(&cont)
   { }

   nested_back_inserter& operator=(typename Container::value_type value)
   {
       if(k == n) {
           ++current_;
           k = 0;
       }
       (*container_)[current_].push_back(value);
       ++k;
       return *this;
   }

   nested_back_inserter& operator*()
   {
       return *this;
   } 

   nested_back_inserter& operator++()
   {
       return *this;
   }

   nested_back_inserter& operator++(int)
   {
       return *this;
   }
};

int main()
{
    int arr[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
    std::vector<std::vector<int>> v;

    for(unsigned i = 0; i < 3; ++i) {
        std::vector<int> vi;
        v.push_back(vi);
    }

    typedef std::vector<std::vector<int>> nested;
    typedef std::vector<int> cont;
    std::copy(arr[0], arr[2] + 3, nested_back_inserter<nested, cont, 3>(v));

    for(auto it = v.begin(); it != v.end(); ++it) {
        std::cout << "{";
        for(auto it2 = it->begin(); it2 != it->end(); ++it2) {
                std::cout << *it2 << ", ";
        }
        std::cout << "}\n";
    }

    return 0;
}

Note specifically the uglyness in std::copy(arr[0], arr[2] + 3, ...);.

Due to tiredness, I take no responsibility for any off-by-one errors or other oddness that could occur with this. It should give you an idea of how to implement something like this however.

Upvotes: 1

n. m. could be an AI
n. m. could be an AI

Reputation: 119847

You will have to write your own iterator that, upon dereference, produces a helper object that, upon assignment, copies a one-dimensional array to a vector with std::copy (and for copying to the opposite direction, another iterator that does the opposite). Basically, take a look at how back_insert_iterator is implemented, and do pretty much the same, only instead of push_back call std::copy.

Personally I think it's not worth it. I'd just use a for loop for the outer copy. You already have one, just add std::copy to its body, right after resize.

Note that if you resize your vectors beforehand, you don't need std::back_inserter as it will allocate yet more storage. Use begin instead.

Upvotes: 0

Related Questions