Dear cpp-ians,
I am working with a structure:
struct meta_segment
{
long double id;
long double num;
long double mean;
bool done;
};
but I want to store multiple elements for some of the elements of my
structure. I was thinking of using arrays in my structure. E.g., when I
have 5 elements for 'num' and 'mean':
struct meta_segment
{
long double id;
long double num[5];
long double mean[5];
bool done;
};
The problem is that only at run-time the program knows how long my
'num' and 'mean' will be. So what I want to do is make a structure,
where I can incorporate the length into the structure and use that
length as an argument.
struct meta_segment
{
long double id;
long double num[NbElements];
long double mean[NbElements];
bool done;
};
I something like this possible? Or should I look for other solutions to
solve this problem?
Thank you very much in advance,
Stef 16 1712
"steflhermitte" <st***************@agr.kuleuven.ac.be> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com... Dear cpp-ians,
I am working with a structure:
struct meta_segment { long double id; long double num; long double mean; bool done; };
but I want to store multiple elements for some of the elements of my structure. I was thinking of using arrays in my structure. E.g., when I have 5 elements for 'num' and 'mean':
struct meta_segment { long double id; long double num[5]; long double mean[5]; bool done; };
The problem is that only at run-time the program knows how long my 'num' and 'mean' will be. So what I want to do is make a structure, where I can incorporate the length into the structure and use that length as an argument.
struct meta_segment { long double id; long double num[NbElements]; long double mean[NbElements]; bool done; };
I something like this possible? Or should I look for other solutions to solve this problem?
Thank you very much in advance, Stef
Why dont you use a class and allocate the memory in the constructor? You
dont have to add any methods, and making the variables public means you can
use the class in exactly the same way you would use your structure.
Allan
steflhermitte wrote: Dear cpp-ians,
[snip] I something like this possible? Or should I look for other solutions to solve this problem?
You want a dynamically sizeable array.
Use std::vector for this.
#include <vector>
struct meta_segment
{
long double id;
std::vector< long double > num;
std::vector< long double > mean;
bool done;
};
Now the num and mean members can grow dynamically as needed.
int main()
{
meta_segment TheData;
TheData.num.push_back( 5.0 ); // add 1 entry to num
TheData.num.push_back( 7.0 ); // and another one
for( int i = 0; i < 200; ++i ) // what the heck ...
TheData.num.push_back( i ); // ... add a lot of them
}
--
Karl Heinz Buchegger kb******@gascad.at
steflhermitte wrote: Dear cpp-ians,
I am working with a structure:
struct meta_segment { long double id; long double num; long double mean; bool done; };
but I want to store multiple elements for some of the elements of my structure. I was thinking of using arrays in my structure. E.g., when I have 5 elements for 'num' and 'mean':
struct meta_segment { long double id; long double num[5]; long double mean[5]; bool done; };
The problem is that only at run-time the program knows how long my 'num' and 'mean' will be. So what I want to do is make a structure, where I can incorporate the length into the structure and use that length as an argument.
struct meta_segment { long double id; long double num[NbElements]; long double mean[NbElements]; bool done; };
I something like this possible? Or should I look for other solutions to solve this problem?
You can use std::vector:
#include <vector>
#include <iostream>
struct meta_segment
{
long double id;
std::vector<long double> num;
std::vector<long double> mean;
bool done;
};
int main()
{
meta_segment seg;
seg.num.push_back(12345);
seg.num.push_back(54321);
std::cout << seg.num[1] << '\n';
}
steflhermitte wrote: The problem is that only at run-time the program knows how long my 'num' and 'mean' will be. So what I want to do is make a structure, where I can incorporate the length into the structure and use that length as an argument.
use std::vector
"steflhermitte" <st***************@agr.kuleuven.ac.be> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com... Dear cpp-ians,
I am working with a structure:
struct meta_segment { long double id; long double num; long double mean; bool done; };
but I want to store multiple elements for some of the elements of my structure. I was thinking of using arrays in my structure. E.g., when I have 5 elements for 'num' and 'mean':
struct meta_segment { long double id; long double num[5]; long double mean[5]; bool done; };
The problem is that only at run-time the program knows how long my 'num' and 'mean' will be. So what I want to do is make a structure, where I can incorporate the length into the structure and use that length as an argument.
struct meta_segment { long double id; long double num[NbElements]; long double mean[NbElements]; bool done; };
I something like this possible? Or should I look for other solutions to solve this problem?
Thank you very much in advance, Stef
Arrays in structs or classes are a poor idea.
(No copy constructor, fixed size, etc.)
If you need variable-length members in structs,
use std::vector instead of arrays:
#include <iostream>
#include <iomanip>
#include <vector>
using std::vector;
using std::cout;
using std::endl;
using std::setprecision;
struct meta_segment
{
long double id;
std::vector<long double> num;
std::vector<long double> mean;
bool done;
};
int main()
{
meta_segment S;
S.num.push_back(3278.2054);
S.num.push_back(7284.0355);
S.mean.push_back(6342.3967);
S.mean.push_back(3968.2853);
cout << "S.num[0] = " << setprecision(10) << S.num[0] << endl;
cout << "S.num[1] = " << setprecision(10) << S.num[1] << endl;
cout << "S.mean[0] = " << setprecision(10) << S.mean[0] << endl;
cout << "S.mean[1] = " << setprecision(10) << S.mean[1] << endl;
return 0;
}
--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
"Robbie Hatley" <lonewolfintj at pacbell dot net> wrote in message
news:42**********@spool9-west.superfeed.net Arrays in structs or classes are a poor idea. (No copy constructor, fixed size, etc.)
What do you mean by no copy constructor? The array itself doesn't have one
of course, but array members are successfully copied when one struct
containing an array is used to initialise another, e.g.,
#include <iostream>
struct Test
{
int x[5];
};
int main()
{
Test t1;
for(int i=0; i<5; ++i)
t1.x[i]=i;
Test t2(t1);
for(int i=0; i<5; ++i)
std::cout << t2.x[i] << std::endl;
return 0;
}
--
John Carson
> I something like this possible?
Yes, its definately possible. Or should I look for other solutions to solve this problem?
IMHO, yes. Use std::vector.
<snip about NbElements is not an integral constant expression> struct meta_segment { long double id; long double num[NbElements]; long double mean[NbElements]; bool done; };
I something like this possible? Or should I look for other solutions to solve this problem?
Some C++ compilers allow C99's flexible array members as an extension.
If your compiler does, you can try:
struct meta_stuff {
long double num;
long double mean;
};
struct meta_segment {
bool done;
long double id;
meta_stuff stuff[];
};
meta_segment *m = (meta_segment*)malloc(sizeof(meta_segment) +
sizeof(meta_stuff)*NbElements);
Thanks folks, you helped me a lot!
I'm am not an experienced c++-user, so things that are evident are not
so evident for me.
I opted for making a class. I made:
TEST.H
------------------------------------------------
#ifndef TEST_H_
#define TEST_H_
using namespace std;
namespace test
{
class metasegment
{
public:
// constructor
metasegment(unsigned int NbLayers);
// porperties
int id;
int *num;
int *mean;
bool done;
};
};
#endif
------------------------------------------------
TEST.CPP
------------------------------------------------
#include "test.h"
#include <iostream>
#include <stdlib.h>
namespace test
{
// constructor and destructor
metasegment::metasegment(unsigned int NbLayers)
{
id=0;
num = new int[NbLayers];
mean = new int[NbLayers];
done=0;
}
};
------------------------------------------------
Now I want to make a vector based on this class:
vector <metasegment(10)> testvector;
but is does not work. I assume I have to work with typedef, but I don't
know how to solve this problem. Any advice?
Thanks again for your help!
Kind regards,
Stef
steflhermitte wrote: Thanks folks, you helped me a lot!
I'm am not an experienced c++-user, so things that are evident are not so evident for me.
Then this is just another reason for using std::vector instead of fiddling
around with raw arrays. std::vector is a class that encapsulates the nasty
details about arrays, especially the dynamic memory handling.
I opted for making a class. I made:
TEST.H ------------------------------------------------ #ifndef TEST_H_ #define TEST_H_
using namespace std; namespace test { class metasegment { public: // constructor metasegment(unsigned int NbLayers); // porperties int id; int *num; int *mean; bool done; }; }; #endif ------------------------------------------------
TEST.CPP ------------------------------------------------ #include "test.h" #include <iostream> #include <stdlib.h>
namespace test { // constructor and destructor
No, this is just a constructor.
metasegment::metasegment(unsigned int NbLayers) { id=0; num = new int[NbLayers]; mean = new int[NbLayers]; done=0; }
If you do the dynamic memory yourself, you must provide a destructor that
properly destroys the dynamic arrays. Futher, you need to provide a
user-defined copy constructor and assignment operator. The compiler
generates those if you don't write you own, but in this case, the
compiler-generated ones don't do what you want.
Again, std::vector would handle this for you.
}; ------------------------------------------------
Now I want to make a vector based on this class:
vector <metasegment(10)> testvector;
but is does not work.
Between the < and > has to be a class. metasegment(10) is not a class.
I assume I have to work with typedef, but I don't know how to solve this problem. Any advice?
Just do:
vector<metasegment> testvector; //define the vector
testvector.push_back(metasegment(10)); //append an instance of your class
//to it, initialized with 10.
steflhermitte wrote: Thanks folks, you helped me a lot!
I'm am not an experienced c++-user, so things that are evident are not so evident for me.
I opted for making a class.
You opted for the worst version you could do.
namespace test { class metasegment { public: // constructor metasegment(unsigned int NbLayers); // porperties int id; int *num; int *mean; bool done; }; };
That class is incomplete.
You are missing:
* a destructor
* a copy constructor
* an assignment operator
thus ...
Now I want to make a vector based on this class:
vector <metasegment(10)> testvector;
but is does not work.
.... this does not work.
I assume I have to work with typedef, but I don't know how to solve this problem. Any advice?
Look up the 'Rule of three'.
Then implement
* a destructor
* a copy constructor
* an assignment operator
if you implement them correctly, it will work.
Or save yourself all the hassle and use a std::vector as was
suggested by lots of replies.
--
Karl Heinz Buchegger kb******@gascad.at
"steflhermitte" <st***************@agr.kuleuven.ac.be> skrev i en meddelelse
news:11*********************@g14g2000cwa.googlegro ups.com... Dear cpp-ians,
I am working with a structure:
struct meta_segment { long double id; long double num; long double mean; bool done; };
but I want to store multiple elements for some of the elements of my structure. I was thinking of using arrays in my structure. E.g., when I have 5 elements for 'num' and 'mean':
struct meta_segment { long double id; long double num[5]; long double mean[5]; bool done; };
The problem is that only at run-time the program knows how long my 'num' and 'mean' will be. So what I want to do is make a structure, where I can incorporate the length into the structure and use that length as an argument.
struct meta_segment { long double id; long double num[NbElements]; long double mean[NbElements]; bool done; };
I something like this possible? Or should I look for other solutions to solve this problem?
Thank you very much in advance, Stef
You should definitely use a std::vector.
/Peter
John Carson wrote: Robbie Hatley wrote: Arrays in structs or classes are a poor idea. (No copy constructor, fixed size, etc.)
What do you mean by no copy constructor? The array itself doesn't have one of course, but array members are successfully copied when one struct containing an array is used to initialise another
My mistake. I was confusing arrays in classes with arrays in
std::containers (which isn't allowed, because assignment isn't
defined for arrays).
On studying the standard, I see in 12.8, clause 8, paragraph 3,
regarding requirments for class implicit copy constructors:
"if the subobject is an array, each element is copied,
in the manner appropriate to the element type"
I find this sort of amusing, because that means if type
Type1 is a class with an array member, I can say:
Type1 t1;
Type1 t2(t1);
However, I'm not allowed to do any of THESE things:
typedef int ArrayOfFiveInts[5];
ArrayOfFiveInts t1 = {9, 1, 1, 17, 4};
// Error, assignment not allowed:
ArrayOfFiveInts t2 = t1;
// Error, cast not allowed:
ArrayOfFiveInts t3 = ArrayOfFiveInts (t1);
// Error, initialization not allowed:
ArrayOfFiveInts t4(t1);
Which I think is dumb. Why not allow those things?
The following works, of course, but is very ugly:
ArrayOfFiveInts t5;for(int i=0;i<5;++i)t5[i]=t1[i];
--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
John Carson wrote: Robbie Hatley wrote: Arrays in structs or classes are a poor idea. (No copy constructor, fixed size, etc.)
What do you mean by no copy constructor? The array itself doesn't have one of course, but array members are successfully copied when one struct containing an array is used to initialise another
My mistake. I was confusing arrays in classes with arrays in
std::containers (which isn't allowed, because assignment isn't
defined for arrays).
On studying the standard, I see in 12.8, clause 8, paragraph 3,
regarding requirments for class implicit copy constructors:
"if the subobject is an array, each element is copied,
in the manner appropriate to the element type"
I find this sort of amusing, because that means if type
Type1 is a class with an array member, I can say:
Type1 t1;
Type1 t2(t1);
However, I'm not allowed to do any of THESE things:
typedef int ArrayOfFiveInts[5];
ArrayOfFiveInts t1 = {9, 1, 1, 17, 4};
// Error, assignment not allowed:
ArrayOfFiveInts t2 = t1;
// Error, cast not allowed:
ArrayOfFiveInts t3 = ArrayOfFiveInts (t1);
// Error, initialization not allowed:
ArrayOfFiveInts t4(t1);
Which I think is dumb. Why not allow those things?
The following works, of course, but is very ugly:
ArrayOfFiveInts t5;for(int i=0;i<5;++i)t5[i]=t1[i];
--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
"Robbie Hatley" <lonewolfintj at pacbell dot net> wrote: On studying the standard, I see in 12.8, clause 8, paragraph 3, regarding requirments for class implicit copy constructors:
"if the subobject is an array, each element is copied, in the manner appropriate to the element type"
I find this sort of amusing, because that means if type Type1 is a class with an array member, I can say:
Type1 t1; Type1 t2(t1);
However, I'm not allowed to do any of THESE things:
typedef int ArrayOfFiveInts[5]; ArrayOfFiveInts t1 = {9, 1, 1, 17, 4};
// Error, assignment not allowed: ArrayOfFiveInts t2 = t1;
This is not an assignment.
// Error, cast not allowed: ArrayOfFiveInts t3 = ArrayOfFiveInts (t1);
This would be the same as the one before, just with an additional copy.
// Error, initialization not allowed: ArrayOfFiveInts t4(t1);
One reason why typedefs for arrays are usually avoided.
Which I think is dumb. Why not allow those things?
Good question.
The following works, of course, but is very ugly:
ArrayOfFiveInts t5;for(int i=0;i<5;++i)t5[i]=t1[i];
Or:
ArrayOfFiveInts t6 = { t1[0], t1[1], t1[2], t1[3], t1[4] };
Robbie Hatley wrote: I find this sort of amusing, because that means if type Type1 is a class with an array member, I can say:
Type1 t1; Type1 t2(t1);
However, I'm not allowed to do any of THESE things:
typedef int ArrayOfFiveInts[5]; ArrayOfFiveInts t1 = {9, 1, 1, 17, 4};
// Error, assignment not allowed: ArrayOfFiveInts t2 = t1;
// Error, cast not allowed: ArrayOfFiveInts t3 = ArrayOfFiveInts (t1);
// Error, initialization not allowed: ArrayOfFiveInts t4(t1);
Which I think is dumb. Why not allow those things?
Because of The Rule. In the above expressions, 't1' is
converted to a pointer to int, before the '=' is processed.
To allow your code, you would either have to revoke The Rule
(which I think would cause too much incompatibility with
existing code), or allow this:
int *ptr = foo();
ArrayOfFiveInts t2 = ptr;
which seems possible but I think it is dangerous. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: BigDadyWeaver |
last post by:
I am using the following code in asp to define a unique and
unpredictable record ID in Access.
<%
'GENERATE UNIQUE ID
Function genguid()
Dim Guid
guid =...
|
by: MLH |
last post by:
I'm working with lots of long strings now, it seems.
I have to import them & parse them constantly. The
A97 memo field type supports only 32768 chars.
What happens when this is processed...
Dim...
|
by: Panchal V |
last post by:
I want to access a variable length record in C, the format is as
follows :
+---+---+-----------+
| A | L | D A T A |
+---+---+-----------+
A - Some Data (1 BYTE)
L - Length the Data that...
|
by: Luiz Antonio Gomes Pican?o |
last post by:
How i can store a variable length data in file ?
I want to do it using pure C, without existing databases.
I'm thinking to use pages to store data.
Anyone has idea for the file format ?
I...
|
by: Skybuck Flying |
last post by:
Hi,
I think I might have just invented the variable bit cpu :)
It works simply like this:
Each "data bit" has a "meta data bit".
The meta data bit describes if the bit is the ending bit...
|
by: gmelcer |
last post by:
Hi, I need to use the data type "struct" in a class. Could someone show
me a simple example of how to use stuct in a class and access the data
type which is declared in the private section of the...
|
by: chandra.krothapalli |
last post by:
Hi,
I am writing a program to read database logs using db2Readlog/
db2ReadLogNoConn API.
I am able to parse the data of "FIXED format data" and link them to
appropriate columns for a given...
|
by: UncleRic |
last post by:
Does anyone know how to return a usable array of pointers to structures?
Here's the structure:
typedef struct {
char* name; // '\0'-terminated C string
int number;
} SomeSeq;
|
by: =?Utf-8?B?SXNsYXkgUm9kcmlndWV6IEpyLg==?= |
last post by:
How do you share glogal data stuctures between VB.net and C++ or C# ?
Thanks,
Islay
--
Islay
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |