CPP 核心编程9-STL案例-员工分组

本文介绍了一个使用C++实现的员工信息管理程序,包括员工基本信息的创建、随机分配到不同部门以及按部门展示员工信息等功能。通过具体代码示例展示了如何利用STL容器与算法进行高效的数据管理和展示。

#include "iostream"
#include "vector"
#include "map"

#define PLAN_DEPT 1
#define ART_DEPT 2
#define DEVELOP_DEPT 3

using namespace std;

class Worker {
public:
    string m_Name;
    int m_Salary;
};

void printWorker(const vector<Worker> &v) {
    for (vector<Worker>::const_iterator it = v.begin(); it != v.end(); it++) {
        cout << "姓名:" << it->m_Name << " 工资" << it->m_Salary << "元" << endl;
    }
}

void createWorker(vector<Worker> &v) {
    string nameSeed = "ABCDEFGHIJ";
    for (int i = 0; i < 10; i++) {
        Worker w;
//        w.m_Name = "员工" + nameSeed[i];//这里不能直接拼接字面量,否则打印的姓名结果如下
/*姓名:_M_realloc_insert 工资9589元
姓名:M_realloc_insert 工资8360元
姓名:_realloc_insert 工资11986元
姓名:realloc_insert 工资14512元
姓名:ealloc_insert 工资9893元
姓名:alloc_insert 工资14757元
姓名:lloc_insert 工资15821元
姓名:loc_insert 工资15889元
姓名:oc_insert 工资11668元
姓名:c_insert 工资14681元
*/
        w.m_Name = string("员工") + nameSeed[i];
        w.m_Salary = rand() % 8000 + 8000;
        v.push_back(w);
    }
}

void setRandGroup(vector<Worker> &v, multimap<int, Worker> &m) {
    for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++) {
        int deptNum = rand() % 3 + 1;
        m.insert(make_pair(deptNum, *it));
    }
}

void doPrintGroup(string deptName, int deptId, const multimap<int, Worker> &m) {
    cout << deptName << ":" << endl;
    multimap<int, Worker>::const_iterator it = m.find(deptId);
    int num = m.count(deptId);
    while (num-- > 0) {
        cout << "人员:" << it->second.m_Name << ",薪资:" << it->second.m_Salary << "元" << endl;
        it++;
    }
}

void printGroup(const multimap<int, Worker> &m) {
    doPrintGroup("策划部门", PLAN_DEPT, m);
    doPrintGroup("美术部门", ART_DEPT, m);
    doPrintGroup("研发部门", DEVELOP_DEPT, m);
}

void test() {
    //创建员工
    vector<Worker> v;
    createWorker(v);
    printWorker(v);
    //员工分组
    multimap<int, Worker> mmap;
    setRandGroup(v, mmap);
    printGroup(mmap);
}

int main() {
    srand((unsigned int) time(NULL));
    test();
    return 0;
}
姓名:员工A 工资10578元
姓名:员工B 工资10025元
姓名:员工C 工资14594元
姓名:员工D 工资9416元
姓名:员工E 工资13054元
姓名:员工F 工资15969元
姓名:员工G 工资8737元
姓名:员工H 工资12460元
姓名:员工I 工资14454元
姓名:员工J 工资12137元
策划部门:
人员:员工A,薪资:10578元
人员:员工H,薪资:12460元
美术部门:
人员:员工C,薪资:14594元
人员:员工E,薪资:13054元
研发部门:
人员:员工B,薪资:10025元
人员:员工D,薪资:9416元
人员:员工F,薪资:15969元
人员:员工G,薪资:8737元
人员:员工I,薪资:14454元
人员:员工J,薪资:12137元

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值