Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Recode the roster example we studied in class such that it prints the list of classes each student is enrolled in. Here is an example
Recode the roster example we studied in class such that it prints the list of classes each student is enrolled in. Here is an example printout:
All Students last name, first name: courses enrolled Kathleen Anderson: CS1 CS3 CS4 Gerald Edwards: CS1 CS2 CS3 Mary Price: CS1 ...
The project should be done using associative containers and class Student to hold the name of the student. Specifically, you need to use a map keyed by the class Student while value could be a list of classes the student is enrolled in. The map should be the ordered map. That is, the data structure to be used in as follows: map
Here is the source file. rosterObject.cpp
#include#include #include #include #include
#include using std::ifstream; using std::string; using std::getline; using std::list; using std::vector; using std::cout; using std::endl; using std::move; class Student{ public: Student(string firstName, string lastName): firstName_(firstName), lastName_(lastName) {} // move constructor, not really needed, generated automatically Student(Student && org): firstName_(move(org.firstName_)), lastName_(move(org.lastName_)) {} // force generation of default copy constructor Student(const Student & org) = default; string print() const {return firstName_ + ' ' + lastName_;} // needed for unique() and for remove() friend bool operator== (Student left, Student right){ return left.lastName_ == right.lastName_ && left.firstName_ == right.firstName_; } // needed for sort() friend bool operator< (Student left, Student right){ return left.lastName_ < right.lastName_ || (left.lastName_ == right.lastName_ && left.firstName_ < right.firstName_); } private: string firstName_; string lastName_; }; // reading a list from a fileName void readRoster(list & roster, string fileName); // printing a list out void printRoster(const list & roster); int main(int argc, char* argv[]){ if (argc <= 1){ cout << "usage: " << argv[0] << " list of courses, dropouts last" << endl; exit(1);} // vector of courses of students vector > courseStudents; for(int i=1; i < argc-1; ++i){ list
roster; readRoster(roster, argv[i]); cout << " " << argv[i] << " "; printRoster(roster); courseStudents.push_back(move(roster)); } // reading in dropouts list dropouts; readRoster(dropouts, argv[argc-1]); cout << " dropouts "; printRoster(dropouts); list allStudents; // master list of students for(auto& lst : courseStudents) allStudents.splice(allStudents.end(),lst); cout << " all students unsorted "; printRoster(allStudents); allStudents.sort(); // sorting master list cout << " all students sorted "; printRoster(allStudents); allStudents.unique(); // eliminating duplicates cout << " all students, duplicates removed "; printRoster(allStudents); for (const auto& str : dropouts) // removing individual dropouts allStudents.remove(str); cout << " all students, dropouts removed "; printRoster(allStudents); } void readRoster(list & roster, string fileName){ ifstream course(fileName); string first, last; while(course >> first >> last) roster.push_back(Student(first, last)); course.close(); } // printing a list out void printRoster(const list & roster){ for(const auto& student : roster) cout << student.print() << endl; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started