"steflhermi tte" <st************ ***@agr.kuleuve n.ac.be> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.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::setprecisi on;
struct meta_segment
{
long double id;
std::vector<lon g double> num;
std::vector<lon g double> mean;
bool done;
};
int main()
{
meta_segment S;
S.num.push_back (3278.2054);
S.num.push_back (7284.0355);
S.mean.push_bac k(6342.3967);
S.mean.push_bac k(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 =----