Write a program where you will have a vector V where each entry is a pointer to
ID: 3791209 • Letter: W
Question
Write a program where you will have a vector V where each entry is a pointer to a vector of strings. This means that each entry in V points to a vector of strings. Your program will then read input strings. For each string, if the number of characters in the string is N, then add it to the string vector in entry V[N]. Be sure to allocate the string vector in each entry as needed. The input string will have a maximum of 10 characters so you can initialize V with 10 entries. Do not add repeated entries. Stop when string "quit" is read. String "quit" should not be processed. Then output the contents of each V entry in order from V[0] to V[10], separated by spaces within the same V entry and by a new line when switching to the next entry. Skip empty entries.
Sample Input:
cat
house
plane
cat
dog
pet
vehicle
run
safe
people
quit
Sample Output:
cat dog pet run
safe
house plane
people
vehicle
Explanation / Answer
A)
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
ifstream file("maze.txt");
if (file) {
vector<char> vec(istreambuf_iterator<char>(file), (istreambuf_iterator<char>()));
vector<char> path;
int x = 11;
char entrance = vec.at(10);
char firstsquare = vec.at(x);
if (entrance == 'S') {
path.push_back(entrance);
}
for (x = 11; isalpha(firstsquare); x++) {
path.push_back(firstsquare);
}
for (int i = 0; i < path.size(); i++) {
cout << path[i] << " ";
}
cout << endl;
return 0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.