is this typedef valid?

L

lou zion

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]=InVals;
}
}

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
 
I

Ioannis Vranos

lou said:
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]=InVals;
}
}

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]=InVals;
}


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]=InVals;
}
 
V

Victor Bazarov

lou zion said:
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]=InVals;
}
}

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
 
L

lou zion

Victor Bazarov said:
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
 
E

E. Robert Tisdale

Ioannis said:
lou said:
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]=InVals;
}


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().
 
I

Ioannis Vranos

E. Robert Tisdale said:
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().

?
 

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

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,654
Latest member
LannySinge

Latest Threads

Top