C++ Question
Design and implement a class that stores a list of students.
Each student has the list of
courses he/she registered for the semester. You will need a
container of containers to hold this list
of students. Choose the optimal container for this exercise, noting
that no duplicates should be
permitted. Use STL classes with iterators to navigate the list.
Develop a test program, which allows
options to add, remove, and list students and their associated
course lists. Also include a function
that displays the first course name in each list (the function must
use iterators).
Please do not use map and use namespace std.
Since duplicates are not allowed, and map should not be used, std::set may be the right choice for this. Here is one of possible implementations:
--------------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <set>
using namespace std;
class studentList{
set<pair<string, set<string> >> s; //set of pair
of name and set of courses
public:
void addStudent();//function to add new student with his
courses
void removeStudent();//remove a student
void listStudents();//list all students along with their
courses
void displayFirstCourse();//display first courses of each
students
};
void studentList::addStudent(){
cout << endl;
cout<<"Enter name of the student : ";
string name;
cin.ignore();
getline(cin, name);//to take input of string with spaces
cout << "Enter number of courses he/she registered :
\n";
int n;
cin >> n;
cout << "Enter names of courses he/she registered followed
by enter : \n";
set<string> courses;
cin.ignore();
for(int i = 0; i < n; ++i){
string s;
getline(cin, s);
courses.insert(s);
}
s.insert(make_pair(name, courses));
cout << endl;
}
void studentList::removeStudent(){
cout << endl;
cout << "Enter name of the student to remove : ";
string name;
cin.ignore();
getline(cin, name);
auto it = s.begin();
for(; it != s.end(); ++it){
if(it->first == name)break;
}
if(it != s.end()) s.erase(it);
cout << endl;
}
void studentList::listStudents(){
cout << endl;
auto it = s.begin();
for(; it != s.end(); ++it){
cout << "Name of the student : " << it->first
<< endl;
cout << "List of courses by " << it->first <<
" :" << endl;
auto it2 = it->second.begin();
for(; it2 != it->second.end(); ++it2){
cout << *it2 << endl;
}
cout << "\n";
}
cout << endl;
}
void studentList::displayFirstCourse(){
cout << endl;
cout << "First course name in each List : \n";
auto it = s.begin();
for(; it != s.end(); ++it){
auto it2 = it->second.begin();
cout << *it2 << endl;
}
cout << endl;
}
int main(){
studentList S;
while(true){
cout << "-------Menu--------\n";
cout << "1. Add Student\n";
cout << "2. Remove Student\n";
cout << "3. Display Students and their courses\n";
cout << "4. Display first courses in each list\n";
cout << "5. exit\n";
cout << "Enter your choice : ";
int choice;
cin >> choice;
if(choice == 1){
S.addStudent();
}
else if(choice == 2){
S.removeStudent();
}
else if(choice == 3){
S.listStudents();
}
else if(choice == 4){
S.displayFirstCourse();
}
else break;
}
}
C++ Question Design and implement a class that stores a list of students. Each student has...