What's the most efficient way to transform a 2d array into a 2dvector

X

xz

Let's say I have

double a[N][M] = { .......};

How to transform a into a 2D vector

vector< vector<double> > v

?
 
A

Abhishek Padmanabh

Let's say I have

double a[N][M] = { .......};

How to transform a into a 2D vector

vector< vector<double> > v

?

One way to do it would be:

double arr[N][M];
std::vector<std::vector<double> > vecOfVecOfDouble(N);
for(std::vector<std::vector<double> >::size_type i=0;i<N;++i)
{
//vecOfVecOfDouble.reserve(M); //shouldn't make a
difference but might be worth checking?
vecOfVecOfDouble.assign(arr, arr+M);
}

Another option would be to use something like boost::array
(boost::array<boost::array<double, M>, N>) instead of std::vector, if
you don't have the need to have a growing target container. That would
result in an identical memory layout. But if you wanted that why would
you start off with a plain 2D array in the first place.
 
X

xz

But if you wanted that why would
you start off with a plain 2D array in the first place.


If you wanna initialize a 2D vector (12x2) with the following data:

-5, -5,
-5, 105,
105, 105,
105, -5,
60, -5,
60, 40,
70, 40,
70, 60,
30, 60,
30, 40,
40, 40,
40, -5,


How do you do that?
 
B

Brian Tyler

If you wanna initialize a 2D vector (12x2) with the following data:

-5, -5,
-5, 105,
105, 105,
105, -5,
60, -5,
60, 40,
70, 40,
70, 60,
30, 60,
30, 40,
40, 40,
40, -5,


How do you do that?

int arr[2][2]={{-5,-5,},{-5,105}};

You can probably figure out the rest...
 
A

Abhishek Padmanabh

If you wanna initialize a 2D vector (12x2) with the following data:

-5,     -5,
-5,     105,
105,    105,
105,    -5,
60,     -5,
60,     40,
70,     40,
70,     60,
30,     60,
30,     40,
40,     40,
40,     -5,

How do you do that?

If that is the case, then why do you want to use a vector at all? Why
don't you work with the arrays only or something like boost:array?
vectors are not a complete replacement of arrays especially when
arrays know their size (and they don't grow) and elements at the time
of creation.
 
X

xz

If that is the case, then why do you want to use a vector at all? Why
don't you work with the arrays only or something like boost:array?
vectors are not a complete replacement of arrays especially when
arrays know their size (and they don't grow) and elements at the time
of creation.

What if I want to use some function that reads vector instead of
array?
 
C

Christopher

Let's say I have
double a[N][M] = { .......};
How to transform a into a 2D vector
vector< vector<double> > v

One way to do it would be:

double arr[N][M];
std::vector<std::vector<double> > vecOfVecOfDouble(N);
for(std::vector<std::vector<double> >::size_type i=0;i<N;++i)
{
//vecOfVecOfDouble.reserve(M); //shouldn't make a
difference but might be worth checking?
vecOfVecOfDouble.assign(arr, arr+M);
}


Iterating through it is probably _not_ the most efficient way.
An array has iterators too ya know, they just happen to be pointers.

I would suggest using:

"template <class InputIterator>
vector(InputIterator, InputIterator)"

For a nice one liner.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top