473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

is this typedef valid?

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
5 1254
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
"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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steven T. Hatton | last post by:
typedef struct { unsigned char e_ident; Elf32_Half e_type; Elf32_Half e_machine; Elf32_Word e_version; Elf32_Addr e_entry; Elf32_Off e_phoff; Elf32_Off e_shoff; Elf32_Word e_flags;...
7
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type ...
8
by: Fred L. Kleinschmidt | last post by:
I need to know the largets value representable in a variable. However, I do not know the variable's true type - only that it is some kind of int. It may be any of the following: #typedef Newtype...
4
by: Chris | last post by:
I've lurked around long enough... Time to interract =) I'm trying to make sense of the following. I can't quite wrap my head around what this is actually doing: ------------- typedef enum {...
5
by: aekalman | last post by:
Hi all. I'm revising part of a large body of code that runs on a variety of very different targets, and one of the things I'd like to do is move away from certain "#define'd types" in favor of...
30
by: stephen henry | last post by:
Hi all, I have a question that I'm having difficulty answering. If I have a struct: typedef struct my_struct_tag{ struct my_other_struct *other; } my_struct_tag
2
by: Immo Birnbaum | last post by:
Hi, I'm trying to solve a programming lab assignment for my college C programming course, but as they taught us two semesters of Java before teaching us any C, I'm having problems with all the...
6
by: Alex | last post by:
Hello people, I am getting errors from VS2003 when working with typedef'ed types. For example, assume that I have a type T, defined in a 3rd party include file based on some condition #if...
5
by: Jess | last post by:
Hello, I think the syntax of typedef is typedef existing-type new-type However, I've seen a statement typedef string stringarray;
4
by: subramanian100in | last post by:
Consider the program #include <iostream> using namespace std; class Test { public: Test(Test_int c_value)
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.