Access same elements in array/vector through different ways

Hello all,
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
1
2
3
4
5
6
    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:
1
2
    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
Last edited on
References come to mind.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

struct member
{
    int params[6];
    int& height = params[0];
};

int main()
{
    member one;

    one.params[0] = 61;
    std::cout << one.height << '\n';
    std::cout << one.params[0] << '\n';
    
    one.height = 47;
    std::cout << one.height << '\n';
    std::cout << one.params[0] << '\n';
    
}

@Repeater

Thanks a lot. Actually all I need to write a vector of struct to a CSV file which the struct contain over 80 element(fields). Therefore I want some way to access the same element in different ways. When processing, MemberParam.Weight, when write it out, MemberParam[0] so that a simple for loop is good enough.
So, what exactly is wrong with what Repeater said?

1
2
3
4
5
6
// processing
one.Weight= 61;

// write it out
for (int i = 0; i < 6; i++)
    one.params[i]; // i == 1 will be Weight 
you can use an enum to name array/vector locations.
enum locs
{
height,weight, age, ... etc
max_locs //always have a max, many reasons...
};

vector<int> thing(max_locs); //see how max can be handy? It grows automatically if you add to the enum.
thing[weight]; //access 0th element with a useful name.

this may or may not be something akin to what you asked... a simple vector & enum are a very solid data structure for simple tasks...
Last edited on
Topic archived. No new replies allowed.