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
  • 相关阅读:
    POJ 1469 COURSES 二分图最大匹配
    POJ 1325 Machine Schedule 二分图最大匹配
    USACO Humble Numbers DP?
    SGU 194 Reactor Cooling 带容量上下限制的网络流
    POJ 3084 Panic Room 求最小割
    ZOJ 2587 Unique Attack 判断最小割是否唯一
    Poj 1815 Friendship 枚举+求最小割
    POJ 3308 Paratroopers 最小点权覆盖 求最小割
    1227. Rally Championship
    Etaoin Shrdlu
  • 原文地址:https://www.cnblogs.com/yibeimingyue/p/10549099.html
Copyright © 2011-2022 走看看