# 2D vector struct, 1D pointer?

This is a discussion on 2D vector struct, 1D pointer? within the C++ Programming forums, part of the General Programming Boards category; Here what I'm doing/trying: Code: struct myStruct{ int iA; double dB; }; vector< vector<myStruct> > data2D; data2D.push_back(vector <myStruct>());// 0 myStruct ...

1. ## 2D vector struct, 1D pointer?

Here what I'm doing/trying:
Code:
```struct myStruct{ int iA; double dB; };
vector< vector<myStruct> > data2D;

data2D.push_back(vector <myStruct>());// 0
myStruct row; row.iA=1; row.dB=2.3;
data2D[0].push_back(row);// 0,0

data2D.push_back(vector <myStruct>());// 1
row.iA=4; row.dB=5.6;
data2D[1].push_back(row);// 1,0

vector <myStruct> *pData1D = &data2D[0];// must be wrong?

// How do I access iA=1?

pData1D[0].iA // of course not
pData1D[0]->iA // nope
pData1D->[0].iA // and nope```
Thanks

2. Access from a pointer... or just data2D ?
If the later..just use the [] operator...like "data2D[0][0].iA"
else .. " pData1d->at(0).iA "

3. pData1D->at(0).iA

Bingo!
Thank you very much.

4. (*pData1D)[0].iA should also work.
Additionally, it is unclear why you use a pointer when you can use a reference instead:

vector <myStruct>& pData1D = data2D[0];
pData1D[0].iA

5. Alternatively
Code:
`int i = (*pData1D)[0].iA;`

6. Elysia,
Reference = wow.
I actually use them as function parameters already.
But, I never knew anything about them before.
I think References are going to be my new favorite discovery.
Thanks

Popular pages Recent additions