Mega Code Archive

 
Categories / C++ Tutorial / Data Types
 

Manage list of employees based on STL

#include <iostream> #include <string> #include <vector> #include <map> #include <list> #include <algorithm> const unsigned int LENGTH = 5; class EmployeeList {     public:     typedef std::vector<int> grades;     std::map<std::string, grades> roster;     std::list<std::string> waiting_list;     public:     public:     void add(const std::string& name);     void remove(const std::string& name);     void record(const std::string& name,const int grade,const unsigned int assignment_number);     void display();     private:     void newEmployee(const std::string& name)     {         grades no_grades;         roster.insert(         std::pair<std::string, grades>(name, no_grades));     } }; void EmployeeList::add(const std::string& name) {     if (roster.find(name) != roster.end())        return;     if (roster.size() < LENGTH) {        newEmployee(name);     } else {        waiting_list.push_back(name);     } } void EmployeeList::remove(const std::string& name) {     std::map<std::string, grades>::iterator anEmployee =  roster.find(name);     if (anEmployee == roster.end())        return;     roster.erase(name);     if (waiting_list.size() > 0) {         std::string wait_name = waiting_list.front();         waiting_list.pop_front();         newEmployee(wait_name);     } } void EmployeeList::record(const std::string& name,const int grade,const unsigned int assignment_number) {     std::map<std::string, grades>::iterator anEmployee =     roster.find(name);     if (anEmployee == roster.end())     {         std::cerr << "ERROR: No " << name << '\n';         return;     }     if (anEmployee->second.size() <= assignment_number)         anEmployee->second.resize(assignment_number+1);     anEmployee->second[assignment_number] = grade; } void EmployeeList::display() {     std::vector<std::string> sorted_names;     std::map<std::string, grades>::iterator curEmployee;     for (curEmployee = roster.begin();curEmployee != roster.end();++curEmployee){         sorted_names.push_back(curEmployee->first);     }     std::sort(sorted_names.begin(), sorted_names.end());     std::vector<std::string>::const_iterator cur_print;     for (cur_print = sorted_names.begin();cur_print != sorted_names.end();++cur_print)     {        std::cout << *cur_print << '\t';         grades::const_iterator cur_grade;         for (cur_grade = roster[*cur_print].begin();cur_grade != roster[*cur_print].end();++cur_grade)         {             std::cout << *cur_grade << ' ';         }         std::cout << '\n';     } } int main() {     EmployeeList empList;     empList.add("A, S");     empList.add("B, M");     empList.add("J, R");     empList.add("S, J");     empList.add("M, M");     empList.add("G, W");     empList.add("C, W");     std::cout << "Before drop " << std::endl;     empList.display();     std::cout << "\n";     empList.remove("J, R");     std::cout << "After drop " << std::endl;     empList.display();     std::cout << "\n";     for (int i = 0; i < 5; ++i){         empList.record("A, S", i*10+50, i);         empList.record("B, M", i*10+50, i);         empList.record("S, J", i*10+50, i);         empList.record("M, M", i*10+50, i);         empList.record("G, W", i*10+50, i);     }     std::cout << "Final " << std::endl;     empList.display();     std::cout << "\n";     return (0); } Before drop A, S B, M J, R M, M S, J After drop A, S B, M G, W M, M S, J Final A, S 50 60 70 80 90 B, M 50 60 70 80 90 G, W 50 60 70 80 90 M, M 50 60 70 80 90 S, J 50 60 70 80 90