Manage list of employees based on STL : Your list « Data Types « C++ Tutorial

Home
C++ Tutorial
1.Language Basics
2.Data Types
3.Operators statements
4.Array
5.Development
6.Exceptions
7.Function
8.Structure
9.Class
10.Operator Overloading
11.Pointer
12.File Stream
13.template
14.STL Introduction
15.string
16.vector
17.list
18.bitset
19.set multiset
20.valarray
21.queue stack
22.deque
23.map multimap
24.STL Algorithms Modifying sequence operations
25.STL Algorithms Non modifying sequence operations
26.STL Algorithms Binary search
27.STL Algorithms Sorting
28.STL Algorithms Merge
29.STL Algorithms Min Max
30.STL Algorithms Iterator
31.STL Algorithms Heap
32.STL Algorithms Helper
C / ANSI-C
C Tutorial
C++
Visual C++ .NET
C++ Tutorial » Data Types » Your list 
2.38.3.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
2.38.Your list
2.38.1.List of integers
2.38.2.linked list
2.38.3.Manage list of employees based on STL
2.38.4.Implementation of a safe array type vect
2.38.5.Vector with raised exceptions
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.