Vector of pointers to vector<map<int,structure> >

I dont know how to write this.
I have:
1
2
3
4
5
6
vector<int> vector_of_pointers;

void create_vector() {
  vector<map<int,structureA> > my_vector;
  vector_of_pointers.push_back(&my_vector);  // this not compile ....
}

I have a little confussion....
First, where is created 'my_vector' ? Have I a new instance each time >I call the function ? When the program ends, who delete the vectors ? Are they auto-deleted?
Second, I want to store the pointer to a vector to later use it to access to the each member. So , is the next right ?
vector<map<int,structureA> > *a_my_vector = vector_op_pointers.at[x];
Thanks
Last edited on
That's all really dangerous (if you don't use smartptr of course).

1
2
3
4
void create_vector() {
  vector<map<int,structureA> > my_vector;
  vector_of_pointers.push_back(&my_vector);  // this not compile ....
}
Will crash, because 'my_vector' is a local object which becomes invalid when leaving create_vector(). You can only do that with new.

When the program ends, who delete the vectors ? Are they auto-deleted?
Noone deletes those objects for you (ok when the program ends all resources a free'd=dirty)
Ok, I have this new code :
into my private section I have :
1
2
3
vector<  vector<map<unsigned short int,col_data> > *> buffer_vectorS;
vector< map<unsigned short int,col_data> > * buffer_current;
map<unsigned short int,col_data> buffer_current_map;

Later I create a vector of map data, and save its pointer

1
2
3
vector<map<unsigned short int,col_data> > * buffer_vector = new vector<map<unsigned short int,col_data> >;
buffer_vectorS.push_back(buffer_vector);
buffer_current = buffer_vector;


Later I'd want to use the map element of the buffer_current to store data,
buffer_current_map = &buffer_current[index];
But this last does not compile.... I dont know how to write it...
Can you help me ?
Last edited on
'buffer_current' is a pointer to vector< map<unsigned short int,col_data> > hence this expression &buffer_current[index] remains that pointer just shifted by an index

You must dereference the pointer to the vector in order to get the content of the vector:
buffer_current_map = (*buffer_current)[index];

To be honest that construct looks far too complicated/dangerous to get happy with
Topic archived. No new replies allowed.