Vectors

6.3 - Vector value initialization


A vector can also be initialized to hold n number of a same element. For example:

// This creates a vector called smurfs containing {"Smurf", "Smurf", "Smurf", "Smurf", "Smurf"}.
std::vector<std::string> smurfs(5, "Smurf");

// This creates a vector called scores containing {88.8, 88.8, 88.8}.
std::vector<double> scores(3, 88.8);

// This creates a vector called balances containing {0, 0, 0, 0}
// This is because in a vector integers are initialized to 0.
std::vector<int> balances(4);

Your task is to create a vector called temmie_village_residents and initialize it to contain 4 "Temmie" on line number 8.

#include <iostream> #include <string> #include <vector> int main() { // Create and initialize temmie_village_residents after this line! // This insert "Bob" into temmie_village_residents. temmie_village_residents.push_back("Bob"); // Let all the residents of Temmie Village introduce themselves. for (std::string &name : temmie_village_residents) { if (name != "Bob") { std::cout << "Hoii!!! I'm " << name << "!!!" << std::endl; } else { std::cout << "Hi, I'm " << name << "." << std::endl; } } }