【C++ STL小案例】--员工分组

本文主要以员工分组为例介绍STL综合性的案例。目录如下,建议收藏阅读

文章目录

一、案例描述

公司今天招募了10个员工(ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在那个部门工作。

员工信息由:姓名 工资。部门为:策划、美术、研发。

随机给10名员工分配部门和工资。

通过multimap进行信息的插入。key(部门编号)value(员工)

分部门显示员工信息。

二、实现思路分析

1、案例框架搭建

创建10名员工,放到vector中

遍历vector容器,取出每个员工,进行随机分组。

分组后,将员工部门编号为key,具体员工作为value,放入到multimao容器中。

分部门显示员工信息。

cpp 复制代码
    //1、创建员工
    CreatWorker(vWorker);

    //2、分组
    setGroup(vWorker,mWorker);

    //3、分组显示员工
    ShowWorkerByGroup(mWorker);

2、业务函数实现

cpp 复制代码
void CreatWorker(vector<Worker> &v)
{
    string nameSeed = "ABCDEFGHIJ";
    for (int i = 0; i < 10; i++)
    {
        Worker worker;
        worker.m_Name = "员工";
        worker.m_Name += nameSeed[i];

        worker.m_Salary = (rand() % 10000) + 10000;  //10000---19999
        //将员工放进数组中
        v.push_back(worker);
    }

}
void setGroup(vector<Worker> &v, multimap<int ,Worker> &m)
{
    for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
    {
        //产生部门随即编号
        int depID = rand() % 3;

        //将员工插入到分组中
        m.insert(pair<int, Worker>(depID, *it));
    }

}
void ShowWorkerByGroup(multimap<int, Worker> &m)
{
    cout << "========策划部门=========" << endl;
    multimap<int,Worker>::iterator pos = m.find(CEHUA);
    int count = (int)m.count(CEHUA);
    int index = 0;
    for (; pos != m.end() && index < count; pos++,index++)
    {
        cout << "姓名: " << pos->second.m_Name << " 工资: " << pos->second.m_Salary << endl;
    }
     cout << "========美术部门=========" << endl;
    count = (int)m.count(MEISHU);
    index = 0;
    for (; pos != m.end() && index < count; pos++, index++)
    {
        cout << "姓名: " << pos->second.m_Name << " 工资: " << pos->second.m_Salary << endl;
    }
    cout << "========研发部门=========" << endl;
    count = (int)m.count(YANFA);
    index = 0;
    for (; pos != m.end() && index < count; pos++, index++)
    {
        cout << "姓名: " << pos->second.m_Name << " 工资: " << pos->second.m_Salary << endl;
    }
}

三、运行结果

相关推荐
樱木Plus2 天前
深拷贝(Deep Copy)和浅拷贝(Shallow Copy)
c++
blasit4 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
肆忆_5 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星5 天前
虚函数表:C++ 多态背后的那个男人
c++
追逐时光者6 天前
精选 5 款基于 .NET 开源的 Visual Studio 实用插件
visual studio
端平入洛7 天前
delete又未完全delete
c++
端平入洛8 天前
auto有时不auto
c++
郑州光合科技余经理9 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1239 天前
matlab画图工具
开发语言·matlab
dustcell.9 天前
haproxy七层代理
java·开发语言·前端