) write a program that creates a vector of string called "vec". vector "vec" grows and shrinks as the user processes the transactions from a data file called "transaction.txt". the transaction file can only include three types of commands: add, remove, and print. with "add" command, you get two more information, the information you need to put into the vector and the position the information should be inserted. with "remove", you get one more information that indicates which element (index) should be deleted. print command means the content of the vector should be printed on the screen.

Respuesta :

PROGRAM:

#include <iostream>

#include <vector>

#include <fstream>

#include <sstream>

#include <string>

using namespace std;

vector<string> vec(20);

vector<string>::iterator it;

int main() {

  ifstream infile("Transaction.txt");

  string line;

  while(getline(infile,line))

  {

      istringstream iss(line);

      string command;

      iss>>command;

      if(command == "Add")

      {

          string word;

          int position;

          iss>>word>>position;

          vec.insert(vec.begin() + position, word);

      }

      else if(command == "Remove")

      {

          int position;

          iss>>position;

          vec.erase(vec.begin() + position);

      }

      else if(command == "Print")

      {

          for (vector<string>::const_iterator i = vec.begin(); i != vec.end(); ++i)

          {

              if(*i != "")

              {

                      cout << *i << "\n";

              }

          }

      }

  }

  return 0;

}

ACCESS MORE
EDU ACCESS