vector<Citizen> myCitizens (100);
this defines a vector
named myCitizens
comprised of 100 objects of Citizen
class, and each of them is initialized with Citizen()
. Then if I correctly get that what you want to do is to resize vector to contain total number of newBirths
of Citizen
objects you can do it this way:
int new_citizens(int newBirths)
{
// first we need to remove items, after this size() is 0
myCitizens.clear();
// and populate vector now
for(unsigned int i = 0; i<newBirths; i++)
myCitizens.push_back(Citizen());
}
you can also use std::vector::resize
to achieve this:
int new_citizens(int newBirths){
myCitizens.resize(newBirths);
}
note:
using resize
the size of container is adjusted to be exactly what you specify in call to this function: container size will be reduced if current size is greater than what you specified in call to resize
or increased if current size is less than newBirths
.
if however you want to add this number of new items to an existing vector, then you can do it this way:
int new_citizens(int newBirths)
{
// just add new items
for(unsigned int i = 0; i<newBirths; i++)
myCitizens.push_back(Citizen());
}
or again, using resize:
int new_citizens(int newBirths){
myCitizens.resize(myCitizens.size() + newBirths);
}