Write a program that initialises a vector with the following string values: “what” “book” “is” “that” “you” “are” “reading”. Display the contents of the vector on the screen to the user as a question and read in the name of the book the user is reading (you can decide what it will be). Have the program add the name of the book to the vector, word by word. For example, if I am reading “How to learn C++”, the program should add the words, “How” “to” “learn” “C++”, one by one to the vector. Display the new vector.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string>vs{ "what", "book", "is" ,"that", "you", "are" ,"reading" };
vector<string>::iterator it;
for (it = vs.begin(); it != vs.end(); it++)
{
cout << *it << " ";
}
string tmp;
while (cin.get()!='\n')
{
cin >> tmp;
vs.push_back(tmp);
}
for (it = vs.begin(); it != vs.end(); it++)
{
cout << *it << " ";
}
}
Comments
Leave a comment