So I have a vector, that is initally empty, but is surelly gone get filled. It contains instances of structure:
struct some {
int number;
MyClass classInstance;
}
/*Meanwhile later in the code:*/
vector<some> my_list;
When it happens, the I want to add value to vector, I need to enlarge it by one. But of course, I don't want to create any variables to do it. Without this requested, I'd do this:
//Adding new value:
some new_item; //Declaring new variable - stupid, ain't it?
my_list.push_back(new_item); //Copying the variable to vector, now I have it twice!
So, instead, I want to create the new_item
within the vector by increasing its size - have a look:
int index = my_list.size();
my_list.reserve(index+1); //increase the size to current size+1 - that means increase by 1
my_list[index].number = 3; //If the size was increased, index now contains offset of last item
But this does not work! It seems that space is not allocated - and I get vector subscript out of range error.