更多教程笔记请查看我的上一篇文章:点击跳转

c++自学之旅3.0(STL模板)开始!

本次将使用所学过的C++STL模板写两个小案例练习

案例1-评委打分

//案例-评委打分
#include <iostream>
using namespace std;
#include <deque>
#include <algorithm>
#include <vector>
#include <string>
//加随机数种子需要的头文件
#include <time.h>
//对5名选手打分,去除最高分,去除最低分,取平均分
class Person
{
public:
    Person(string name, int score)
    {
        this->m_Name = name;
        this->m_Score = score;
    }
    //姓名
    string m_Name;
    //平均分
    int m_Score;
};
void createPerson(vector<Person> &v)
{
    string nameSeed = "ABCDE";
    for (int i = 0; i < 5; i++)
    {
        //通过字符串拼接对名称赋值
        string name = "player";
        name += nameSeed[i];
        //分数默认为0
        int score = 0;
        Person p(name, score);
        //将创建的person对象,放入到容器中
        v.push_back(p);
    }
}
//输出
void printVector(vector<Person> &v)
{
    cout << "avg: " << endl;
    for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << "name: " << (*it).m_Name << " score: " << (*it).m_Score << endl;
    }
}
//打分
void setScore(vector<Person> &v)
{
    for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
    {
        //将评委的分数放入到deque容器中
        deque<int> d;
        for (int i = 0; i < 10; i++)
        {
            // 随机数:60~100
            int score = rand() % 41 + 60;
            d.push_back(score);
        }
        cout << "player: " << it->m_Name << " score: " << it->m_Score << endl;
        //输出每个评委给的分数
        for (deque<int>::iterator sit = d.begin(); sit != d.end(); sit++)
        {
            cout << *sit << " ";
        }
        cout << endl;
        cout << "------------------------------------" << endl;
        //先排序,再去除最高分和最低分
        sort(d.begin(), d.end());
        //去除最高
        d.pop_back();
        //去除最低
        d.pop_front();

        //取平均分
        int sum = 0;
        for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
        {
            //累加每个评委的分数
            sum += *dit;
        }
        int avg = sum / d.size();

        //将平均分赋值给选手
        it->m_Score = avg;
    }
}
int main()
{
    //随机数种子,加了后就可以让随机数根据时间进行改变
    srand((unsigned int)time(NULL));

    //存放5名选手的容器
    vector<Person> v;
    //创建5名选手
    createPerson(v);
    //打印
    // printVector(v);

    //给5个选手打分
    setScore(v);
    //输出每个选手最后的得分
    printVector(v);
    return 0;
}

案例2—员工分组

//案例2--员工分组
#include <iostream>
using namespace std;
#include <vector>
#include <map>
#include <string>
#include <ctime>
//给予不同的部门编号
// 0是策划
#define CEHUA 0
// 1是美术
#define MEISHU 1
// 2是研发
#define YANFA 2
//随机给10名员工分配部门和工资
class Worker
{
public:
    //员工姓名
    string m_Name;
    //员工年龄
    int m_Salary;
};
void createWorker(vector<Worker> &v)
{
    string nameSeed = "ABCDEFGHIJ";
    for (int i = 0; i < 10; i++)
    {
        //创建一个员工
        Worker worker;
        worker.m_Name = "player";
        //这样的话每个员工的名字就都不一样了
        worker.m_Name += nameSeed[i];
        //工资,随机数 10000 ~ 19000
        worker.m_Salary = rand() % 10000 + 10000;
        //将员工放入到容器中
        v.push_back(worker);
    }
}
//员工分组
void setGroup(vector<Worker> &v, multimap<int, Worker> &m)
{
    for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
    {
        //产生随机部门编号 0 1 2的随机数
        int deptId = rand() % 3;
        //将员工插入到分组中
        // key代表部门编号,value代表具体的人
        m.insert(make_pair(deptId, *it));
    }
}
void showWorkerByGourp(multimap<int, Worker> &m)
{
    cout << "chehua: " << endl;
    //通过find找到策划部门的起始位置
    multimap<int, Worker>::iterator pos = m.find(CEHUA);

    //统计策划部门有多少个人
    int count = m.count(CEHUA);
    int index = 0;
    for (; pos != m.end() && index < count; pos++, index++)
    {
        cout << "name " << pos->second.m_Name << " salary: " << pos->second.m_Salary << endl;
    }
    cout << "--------------------------------" << endl;
    cout << "meisu: " << endl;
    pos = m.find(MEISHU);

    //统计美术部门有多少个人
    count = m.count(MEISHU);
    index = 0;
    for (; pos != m.end() && index < count; pos++, index++)
    {
        cout << "name " << pos->second.m_Name << " salary: " << pos->second.m_Salary << endl;
    }
    cout << "--------------------------------" << endl;
    cout << "yanfa: " << endl;
    pos = m.find(YANFA);

    //统计美术部门有多少个人
    count = m.count(YANFA);
    index = 0;
    for (; pos != m.end() && index < count; pos++, index++)
    {
        cout << "name " << pos->second.m_Name << " salary: " << pos->second.m_Salary << endl;
    }
}
int main()
{
    //创建随机数种子
    srand((unsigned int)time(NULL));
    //创建员工
    vector<Worker> vWorker;
    //创建
    createWorker(vWorker);

    //员工分组
    // multimap容器可以存放相同的key值
    multimap<int, Worker> mWorker;

    //因为要把分组的信息插入到分组函数中,所以需要map
    setGroup(vWorker, mWorker);

    //分组显示员工
    showWorkerByGourp(mWorker);
    //测试
    // for (vector<Worker>::iterator it = vWorker.begin(); it != vWorker.end(); it++)
    // {
    //     cout << "name: " << it->m_Name << " salary: " << it->m_Salary << endl;
    // }

    return 0;
}