I have the following arrangement. I cannot change any of the vectors to array or anything else due to other constraints.
struct shelf {
std::vector<int> books;
}
std::vector<shelf> all_shelves;
all_shelves.reserve(sizeof(shelf)*10);
I reserve the memory for shelves in the last line.
Now how do I reserve the memory that does has the effect like this ?
all_shelves[4].books_per_shelf.reserve(6*sizeof(books));
Currently, I am forced to do this:
struct shelf dummy_shelf;
dummy_shelf.books = 6;
all_shelves.push_back(dummy_shelf);
Can't I access the memory I had reserved earlier ?
I'm aware of the size
and capacity
implementation under the hood in <vector>
. Is there any method that allows me access nth element without pushing back explicitly ?