0

In C++, is there any data structure allow me to access the elements in array/vector through 2 methods? e.g.

int MemberParam[6];

I can access the element like MemberParam[0]..MemberParam[5]. However, if I know

0 is height (in CM)
1 is weight (in kg)
2 is age
3 is b-day (year)
4 is b-day (month)
5 is b-day (day)

Is there any data structure allow me to access the element in this way:

MemberParam.Weight
MemberParam.Height

while MemberParam.Weight actually point to MemberParam[1] ? so that I can access the array/vector element in different way depend on need ?

Thanks.

Regds

LAM Chi-fung

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Chi-fung LAM
  • 195
  • 2
  • 11
  • Create a `struct` instead of an array? An array is a bad way to represent data like this – UnholySheep May 17 '18 at 11:04
  • You may want to look at [this](https://stackoverflow.com/questions/2253878/why-does-c-disallow-anonymous-structs). – user202729 May 17 '18 at 11:07
  • Also in what scenario would you need to access the data using `operator[]`? It would make the code a lot harder to understand than simple member access (such as `.height`) – UnholySheep May 17 '18 at 11:12
  • When I write out the data to a file, with [] and index, I can output by using a simple for loop. Real situation involve vector of structure and in turn has over 80 fields in the struct!!! – Chi-fung LAM May 17 '18 at 13:51
  • You might be interested in having a look at something like Protobuf to serialize your class. – SirDarius May 17 '18 at 14:32
  • Yes right, I tried search and find cxxtools and csv++ but they are for Linux (i am working on VC++) ~_~ – Chi-fung LAM May 18 '18 at 05:06

0 Answers0