I have the following struct in my code which assigns x and y randommly generated integer values:
struct A{
    int x,y;
    A () : x(random_gen_num),y(random_gen_num) {};   
};
I have a vector of objects and need a vector of pointers pointing to each object:
vector<A> a(5);
a.reserve(5);
vector<const A*> apoint(5);
apoint.reserve(5);
for(const A thisA : a){
    apoint.push_back(&thisA);
}
When I try printing them using:
for(unsigned int i = 0; i<5; i++){
    cout<< i <<"\t"<< a[i].x <<"\t" << &a[i]<<"\t" << apoint[i] <<endl;
}
I get all my apoint as NULL pointers as follows:
0       8       0x29b1bd0       0
1       8       0x29b1bd8       0
2       1       0x29b1be0       0
3       8       0x29b1be8       0
4       6       0x29b1bf0       0
The code seems logical to me and works when I say apoint[i] = &a[i] in the for loop, but in the real case this is not valid as the indices of apoint and a might not be same.To my knowledge, I couldn't find an answered question with a similar problem. Is there a better way to insert pointers into vectors? 
 
     
    