Velocity Reviews > C++ > What's the most efficient way to transform a 2d array into a 2dvector

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

xz
Guest
Posts: n/a

 04-04-2008
Let's say I have

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

How to transform a into a 2D vector

vector< vector<double> > v

?

Guest
Posts: n/a

 04-04-2008
On Apr 4, 9:31*pm, xz <(E-Mail Removed)> wrote:
> 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[i].reserve(M); //shouldn't make a
difference but might be worth checking?
vecOfVecOfDouble[i].assign(arr[i], arr[i]+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.

xz
Guest
Posts: n/a

 04-04-2008
> 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?

Brian Tyler
Guest
Posts: n/a

 04-04-2008
On Fri, 04 Apr 2008 11:43:10 -0700, xz wrote:

>> 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?

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

You can probably figure out the rest...

Guest
Posts: n/a

 04-05-2008
On Apr 4, 11:43*pm, xz <(E-Mail Removed)> wrote:
> > 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?

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.

xz
Guest
Posts: n/a

 04-07-2008
On Apr 5, 1:07 am, Abhishek Padmanabh <(E-Mail Removed)>
wrote:
> On Apr 4, 11:43 pm, xz <(E-Mail Removed)> wrote:
>
>
>
> > > 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?

>
> 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?

Christopher
Guest
Posts: n/a

 04-07-2008
On Apr 4, 12:22 pm, Abhishek Padmanabh <(E-Mail Removed)>
wrote:
> On Apr 4, 9:31 pm, xz <(E-Mail Removed)> wrote:
>
> > 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[i].reserve(M); //shouldn't make a
> difference but might be worth checking?
> vecOfVecOfDouble[i].assign(arr[i], arr[i]+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.