Vectors

6.4 - Inserting elements into a vector using push_back


The push_back operation is used on a vector to insert a new element at the back of the vector. For example:

// names starts as {"Tom", "Jerry"}.
std::vector<std::string> names{"Tom", "Jerry"};

// This insert "Spike" to the back of names.
// names will contain {"Tom", "Jerry", "Spike"}
names.push_back("Spike");

Your task is to insert "Spiders" and "Clowns" to the vector fears after line number 8.

#include <iostream> #include <string> #include <vector> int main() { std::vector<std::string> fears{"Ghosts", "Unemployment", "Dishwashers"}; // Insert "Spiders" and "Clowns" after this line! // This code prints out all your deepest fears. std::cout << "I am afraid of:" << std::endl; for (std::string &fear : fears) { std::cout << fear << std::endl; } }