zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Linux网络相关命令firewalld和netfilter、iptables 使用(6/22)
    Linux时间设置与iptables命令
    负载均衡集群ipvsadm命令及基本用法
    LVS原理详解以及部署
    linux比较两个文件的不同(6/21)
    如何使用sql函数平均值、总数、最小值、最大值
    python中数据类型转换
    使用 getopt 处理命令行长参数
    Mysql常用命令行大全
    C#控制台程序使用Log4net日志组件
  • 原文地址:https://www.cnblogs.com/yibeimingyue/p/10549099.html
Copyright © 2011-2022 走看看