By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

is this typedef valid?

P: n/a
hi all,

i want to create a vector where each element is an array of 40 doubles. is
this valid? something like:

void abc( std::vector<double> InVals)
{
typedef double DataSeriesType[40];
std::vector<DataSeriesType> DataSeriesX;

DataSeriesType NewData;
DataSeriesX.push_back(NewData);

for (int i=0; i<(int)InVals.size(); i++) {
DataSeriesX[0][i]=InVals[i];
}
}

the above is not good code, i know, i'm just using it as an example of
syntax, not style.

essentially i want to store an array (or possibly a vector) in a vector. if
i just store a pointer to an array in a vector, what happens if i delete
that element? do i need to deallocate storage somehow? i'd prefer an array,
but if there's a better way to do this, please enlighten.

thnx :)

lou

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
lou zion wrote:
hi all,

i want to create a vector where each element is an array of 40 doubles. is
this valid? something like:

void abc( std::vector<double> InVals)
{
typedef double DataSeriesType[40];
std::vector<DataSeriesType> DataSeriesX;

DataSeriesType NewData;
DataSeriesX.push_back(NewData);

for (int i=0; i<(int)InVals.size(); i++) {
DataSeriesX[0][i]=InVals[i];
}
}

the above is not good code, i know, i'm just using it as an example of
syntax, not style.

essentially i want to store an array (or possibly a vector) in a vector. if
i just store a pointer to an array in a vector, what happens if i delete
that element? do i need to deallocate storage somehow? i'd prefer an array,
but if there's a better way to do this, please enlighten.

#include <vector>

void abc(const std::vector<double> &InVals)
{
using namespace std;

vector<vector<double> >DataSeriesX;

vector<double> NewData(40);

DataSeriesX.push_back(NewData);

for(vector<double>::size_type i=0; i<InVals.size(); ++i)
DataSeriesX[0][i]=InVals[i];
}
or
#include <vector>

void abc(const std::vector<double> &InVals)
{
using namespace std;

typedef vector<double>DataSeriesType;

vector<DataSeriesType> DataSeriesX;

DataSeriesType NewData(40);

DataSeriesX.push_back(NewData);

for(vector<double>::size_type i=0; i<InVals.size(); ++i)
DataSeriesX[0][i]=InVals[i];
}


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #2

P: n/a
"lou zion" <il********@adelphia.net> wrote...
i want to create a vector where each element is an array of 40 doubles. is
this valid?
No. Arrays cannot be stored in a standard container because they do not
satisfy the requirements. The easiest thing is to wrap them in a struct.
something like:

void abc( std::vector<double> InVals)
{
typedef double DataSeriesType[40];
std::vector<DataSeriesType> DataSeriesX;

DataSeriesType NewData;
DataSeriesX.push_back(NewData);

for (int i=0; i<(int)InVals.size(); i++) {
DataSeriesX[0][i]=InVals[i];
}
}

the above is not good code, i know, i'm just using it as an example of
syntax, not style.

essentially i want to store an array (or possibly a vector) in a vector.
if i just store a pointer to an array in a vector, what happens if i
delete that element? do i need to deallocate storage somehow? i'd prefer
an array, but if there's a better way to do this, please enlighten.


If you store a pointer to a dynamic array that you get from using 'new[]',
then just before deleting that element from the vector you will need to
deallocate the memory. A bit of a hassle. Wrapping the array in a struct
is easier.

V
Jul 23 '05 #3

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:u6********************@comcast.com...
"lou zion" <il********@adelphia.net> wrote...
i want to create a vector where each element is an array of 40 doubles.
is this valid?


No. Arrays cannot be stored in a standard container because they do not
satisfy the requirements. The easiest thing is to wrap them in a struct.


hi, thanks to both of you. these'll work well.

lou

Jul 23 '05 #4

P: n/a
Ioannis Vranos wrote:
lou zion wrote:
I want to create a vector
where each element is an array of 40 doubles.


#include <vector>

void abc(const std::vector<double> &InVals)
{
using namespace std;

vector<vector<double> >DataSeriesX;

vector<double> NewData(40);

DataSeriesX.push_back(NewData);

for(vector<double>::size_type i=0; i<InVals.size(); ++i)
DataSeriesX[0][i]=InVals[i];
}


The problem with this solution is that
an object of type vector<vector<double> >
is *not* the object that Lou Zion describes.
A vector<double> has a *flexible* extent
whereas double[40] has a *rigid* extent.

There is also no range checking for 40 < InVals.size().
Jul 23 '05 #5

P: n/a
E. Robert Tisdale wrote:
The problem with this solution is that
an object of type vector<vector<double> >
is *not* the object that Lou Zion describes.

Actually his words were:

"essentially i want to store an array (or possibly a vector) in a vector."

A vector<double> has a *flexible* extent
whereas double[40] has a *rigid* extent.

What do you mean. That the amount of its contents does not change? Well,
if someone wants to preserve the amount of contents, then he should not
change them.

There is also no range checking for 40 < InVals.size().


?


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.