Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Today, we are going to modify our main) function to use the STL vector> template

ID: 3891825 • Letter: T

Question

Today, we are going to modify our main) function to use the STL vector> template to contain all our instances of the WITperson class we instantiate. This will llow it to grow in size without limits (except physical memory!). You will also need to instantiate an iterator for the class so that you can traverse the vector when displaying the list of persons in your database. You will need to do some research on the vectoro class & its iterators to implement the lab. Once you have it working, insert at least 8 instances of people in your database.

Explanation / Answer

#include<iostream>
#include<string>
#include<vector>
#include<fstream>

using namespace std;

//Assuming the following WITperson class

class WITperson {
    private:
      string name;
    public:
      WITperson(string a){
         name = a;
      }
      string getName(){
        return name;
      }
};

class Database{

    private:
        vector<WITperson> list;
        vector<WITperson>::iterator ptr;
    public:
        void add(WITperson a){
            list.push_back(a);          
        }
        void display(){
           for (ptr = list.begin(); ptr < list.end(); ptr++){
               cout << (*ptr).getName() << endl;
           }         
        }
        void writeToFile(){
           cout << "Enter file name:";
           string nm;
           cin >> nm;
           ofstream fout(nm.c_str());
          
           for (ptr = list.begin(); ptr < list.end(); ptr++){
               fout << (*ptr).getName() << endl;
           }
           fout.close();         
        }

};


int main(){

    Database db;
    cout << "Enter number of persons:";
    int n;
    cin >> n;
    for (int i = 0; i<n; i++){
        cout << "Name for " << i << "th person:";
        string nm;
        cin >> nm;
        WITperson p(nm);
        db.add(p);
    }
    db.display();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote