23、STL案例:评委打分

#include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
#include <ctime>

using namespace std;

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 = "选手";
        name += nameSeed[i];

        int score = 0;

        Person p(name, score);
        v.push_back(p);
    }
}

void setScore (vector<Person> &v)
{
    for (vector<Person>::iterator it = v.begin(); it != v.end(); it ++)
    {
        deque<int> d;
        for (int i = 0; i < 10; i ++)
        {
            d.push_front(rand() % 41 + 60);
        }

        sort(d.begin(), d.end());
        d.pop_front();
        d.pop_back();

        int sumScore = 0;
        for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit ++)
        {
            sumScore += *dit;
        }

        int score = sumScore / d.size();
        // cout << score << endl;
        it->m_score = score;
    }

}

void showScore (vector<Person> v)
{
    for (vector<Person>::iterator it = v.begin(); it != v.end(); *it ++)
    {
        cout << it->m_name << it->m_score << endl;
    }
}
int main ()
{
    srand((unsigned int)time(NULL));
    //创建5名选手
    vector <Person> v;
    createPerson(v);

    // for (vector<Person>::iterator it = v.begin(); it != v.end(); it ++)
    // {
    //     cout << (*it).m_name;
    // }
    //给5名选手打分
    setScore(v);
    // for (vector<Person>::iterator it = v.begin(); it != v.end(); it ++)
    // {
    //     cout << it->m_score << endl;
    // }
    //显示最后得分
    showScore(v);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值