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
...
Here is the output for the above example roster files.
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>
roster.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;
// 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);
// master list of students
list allStudents; for(auto& lst : courseStudents) allStudents.splice(allStudents.end(), lst);
cout << "\\\\ all students unsorted \\"; printRoster(allStudents);
// sorting master list
allStudents.sort();
cout << "\\\\ all students sorted \\"; printRoster(allStudents);
// eliminating duplicates
allStudents.unique(); cout << "\\\\ all students, duplicates removed \\"; printRoster(allStudents);
// removing individual dropouts
for (const auto& str : dropouts) allStudents.remove(str);
cout << "\\\\ all students, dropouts removed \\"; printRoster(allStudents);
}
// reading in a file of names into a list of strings
void readRoster(list& roster, string fileName){
ifstream course(fileName);
string first, last;
while(course >> first >> last)
roster.push_back(move(first + ' ' + last));
course.close();
}
// printing a list out
void printRoster(const list& roster){
for(const auto& str : roster)
cout << str << endl;
}
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)
(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