Reusing a vector in C++
- by Bobby
I have a vector declared as a global variable that I need to be able to reuse. For example, I am reading multiple files of data, parsing the data to create objects that are then stored in a vector.
vector<Object> objVector(100);
void main()
{
while(THERE_ARE_MORE_FILES_TO_READ)
{
// Pseudocode
ReadFile();
ParseFileIntoVector();
ProcessObjectsInVector();
/* Here I want to 'reset' the vector to 100 empty objects again */
}
}
Can I reset the vector to be "vector objVector(100)" since it was initially allocated on the stack? If I do "objVector.clear()", it removes all 100 objects and I would have a vector with a size of 0. I need it to be a size of 100 at the start of every loop.