• c++ STL库deque和vector的例子


    头文件wuyong.h:

    #pragma once
    #include<iostream>
    #include<vector>
    #include<deque>
    #include<algorithm>
    #include<string>
    using namespace std;
    class Player
    {
    public:
        Player(){}
        Player(string name, int score) :mName(name), mScore(score){}
        ~Player(){}
    
    public:
        string mName;
        int mScore;
    };
    
    void Creat_Player(vector<Player>& v)
    {
        string nameSeed ="ABCDE";
        for (int i = 0; i < 5; i++)
        {
            Player p;
            p.mName = "选手";
            p.mName += nameSeed[i];
            p.mScore = 0;
            v.push_back(p);
            
        }
    }
    void printscore(int val)
    {
        cout << val << "  ";
    }
    void Set_score(vector<Player>& v)
    {
        for (vector<Player>::iterator it = v.begin(); it != v.end(); it++)
        {
            //对当前学生进行打分
            deque<int> dScore;
            for (int i = 0; i < 10; i++)
            {
                int score = rand() % 41 + 60;
                dScore.push_back(score);
            }
            sort(dScore.begin(), dScore.end());
            //for_each(dScore.begin(), dScore.end(),printscore);
            //cout << endl;
            //去除最高分和最低分
            dScore.pop_front();
            dScore.pop_back();
            int totlescore = 0;
            for (deque<int>::iterator dit = dScore.begin(); dit != dScore.end(); dit++)
            {
                totlescore += (*dit);
                int aveScore = totlescore / dScore.size();
                //保存分数
                (*it).mScore = aveScore;
            }
        }
    }
    bool mycompare(Player& p1,Player& p2)
    {
        return p1.mScore > p2.mScore;
    }
    //分数排名 sort默认从小到大,希望从大到小
    void Print_Rank(vector<Player>& v)
    {
        sort(v.begin(), v.end(), mycompare);
        for (vector<Player>::iterator it = v.begin(); it != v.end(); it++)
        {
            cout << "姓名:" << (*it).mName << "  得分:  " << (*it).mScore << endl;
        }
    }
    View Code

    源文件wuyong.cpp:

    #include"wuyong.h"
    int main()
    {
        vector<Player> vPlist;
        Creat_Player(vPlist);
        Set_score(vPlist);
        Print_Rank(vPlist);
    
        system("pause");
        return 0;
    }
    View Code
  • 相关阅读:
    NC 6系总账凭证联查原始单据
    NCUAP 利用java自带方法实现导入excel取数据
    NC 5导出Excel
    nc 画界面,触发效果(第一种)
    nc6 用业务插件注册来跑按钮事件
    NC 自定义参照类
    SVN忘记登陆用户
    Oracle 忘记sys与system管理员密码重置操作
    OC-之AFNetworking
    OC-之NSDate、万年历
  • 原文地址:https://www.cnblogs.com/yibeimingyue/p/10549099.html
Copyright © 2020-2023  润新知