|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Let's say I have
double a[N][M] = { .......}; How to transform a into a 2D vector vector< vector<double> > v ? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
> 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? |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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... |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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. |
|
![]() |
| Outils de la discussion | |
|
|