PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > What's the most efficient way to transform a 2d array into a 2dvector
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
What's the most efficient way to transform a 2d array into a 2dvector

Réponse
 
LinkBack Outils de la discussion
Vieux 04/04/2008, 17h31   #1
xz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut What's the most efficient way to transform a 2d array into a 2dvector

Let's say I have

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

How to transform a into a 2D vector

vector< vector<double> > v

?
  Réponse avec citation
Vieux 04/04/2008, 18h22   #2
Abhishek Padmanabh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's the most efficient way to transform a 2d array into a 2dvector

On Apr 4, 9:31pm, xz <zhang.xi...@gmail.com> 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.
  Réponse avec citation
Vieux 04/04/2008, 19h43   #3
xz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's the most efficient way to transform a 2d array into a 2dvector

> 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?
  Réponse avec citation
Vieux 04/04/2008, 23h57   #4
Brian Tyler
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's the most efficient way to transform a 2d array into a 2dvector

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...
  Réponse avec citation
Vieux 05/04/2008, 07h07   #5
Abhishek Padmanabh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's the most efficient way to transform a 2d array into a 2dvector

On Apr 4, 11:43pm, xz <zhang.xi...@gmail.com> 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.
  Réponse avec citation
Vieux 07/04/2008, 21h53   #6
xz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's the most efficient way to transform a 2d array into a 2dvector

On Apr 5, 1:07 am, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
> On Apr 4, 11:43 pm, xz <zhang.xi...@gmail.com> 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?

  Réponse avec citation
Vieux 07/04/2008, 22h36   #7
Christopher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's the most efficient way to transform a 2d array into a 2dvector

On Apr 4, 12:22 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
> On Apr 4, 9:31 pm, xz <zhang.xi...@gmail.com> 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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h17.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11516 seconds with 15 queries