zoukankan      html  css  js  c++  java
  • STL

     

     1 #include<deque>
     2 #include<iostream>
     3 using namespace std;
     4 
     5 template<typename T>
     6 void printf(deque<T>& deque_)
     7 {
     8     if (!deque_.empty())
     9     {
    10         for (auto d : deque_)
    11         {
    12             cout << d;
    13         }
    14         cout << endl;
    15     }
    16     else
    17     {
    18         cout << "这个deque是空的啦" << endl;
    19     }
    20 }
    21 
    22 //构造函数
    23 void test01()
    24 {
    25     deque<int> d1;
    26     deque<int> d2(10, 4); // 4444444444
    27     deque<int> d3(d2.begin(), d2.end());
    28     deque<int> d4(d3);//
    29     printf(d4);
    30 }
    31 
    32 //赋值操作
    33 void test02()
    34 {
    35     deque<int> d1;
    36     deque<int> d2;
    37     deque<int> d3;
    38     d1.assign(6, 5);//555555
    39     d2.assign(d1.begin(), d1.end());
    40     d3 = d2;
    41     // empty size resize 略
    42 }
    43 
    44 //deque容器插入与删除
    45 void test03()
    46 {
    47     deque<int> d1;
    48     d1.push_back(3); //3
    49     d1.push_front(2);// 23
    50     d1.push_back(4);// 234
    51     d1.push_front(1);// 1234
    52     printf(d1);// 1234
    53     deque<int>::reverse_iterator  itr = d1.rbegin();
    54     cout << "*itr = " << *itr << endl;
    55     //删除
    56     int val = d1.front();
    57     //删除头元素
    58     d1.pop_front(); printf(d1); // 234
    59     //删除尾元素
    60     d1.pop_back(); printf(d1); // 23
    61 }
    62 int main()
    63 {
    64     test01();
    65     test02();
    66     test03();
    67     return 1;
    68 }

    下面举一个例子:【题目见代码】

    解析:既然要打分去掉最低分和最高分,肯定想到排序,然后去除首尾元素。其实,你也可以用multimap,key值自动排序,只是要去除首key值的时候(erase之后),你要重新获取迭代器,后续所有元素下表减1,这种拷贝对性能损耗大。

    // 评委打分案例(sort 算法排序)
    // 创建5个选手(姓名,得分), 10个评委对5个选手打分
    // 得分规则:去除最高分,去除最低分,去除平均分
    // 按得分对5名选手经行排名
    
    #include<iostream>
    #include<string>
    #include<vector>
    #include<deque>
    #include<algorithm>
    
    using namespace std;
    
    template <typename T1>
    void printf(vector<T1>& vec)
    {
        if (vec.empty())
        {
            cout << "empty! " << endl;
        }
        else
        {
            for (auto& v : vec)
            {
                cout << v << " ";
            }
            cout << endl;
        }
    }
    
    template <typename T2>
    void printf(deque<T2>& vec)
    {
        if (vec.empty())
        {
            cout << "empty! " << endl;
        }
        else
        {
            for (auto& v : vec)
            {
                cout << v << " ";
            }
            cout << endl;
        }
    }
    
    class Player
    {
    public:
        Player(string name, int socre) :name_(name), socre_(socre) {};
    
        string name_;
        int socre_;//根据要求得出的最终评分
    };
    
    void createPalyers(vector<string>& names)
    {
        string str = "ABCDEF";
        string stu = "student";
        for (auto& index:str)
        {
            string name = stu + index;
            names.push_back(name);
        }
    }
    
    bool mycompare(Player& player1,Player& player2)
    {
        return player1.socre_ > player2.socre_;
    }
    
    
    void getSocresAndRank(vector<string>& names)
    {
        vector<Player> players;
        // 给选手随机打分【10个评委10个分数,去除两头,再平均】
        for (auto& name:names)
        {
            deque<int> socre; // 一个选手的10个分数
            for (size_t j = 0; j < 10; j++)
            {
                int temp = rand() % 41 + 60;
                socre.push_back(temp);
            }
            sort(socre.begin(), socre.end());//默认从小到大
            printf(socre);
            socre.pop_back(); //删除最大值
            socre.pop_front();//删除最小值
            printf(socre);
            //求取平均分
            int totalsocres = 0;
            for (deque<int>::iterator itr = socre.begin(); itr!= socre.end(); itr++)
            {
                totalsocres += (*itr);
            }
            int avgsocres = totalsocres/ socre.size();
            Player player(name, avgsocres);//关联一个人 与 它的分数
            players.push_back(player);
            sort(players.begin(), players.end(), mycompare);//【注:这里遍历类,将分数排序,对应的选手也会被排序,类似key-value】
        }
        //打印最终结果
        for (auto&player:players)
        {
            cout << "name: " << player.name_.c_str() << " socre = " << player.socre_ << endl;
        }
    }
    
    int main()
    {
        vector<string> names;
        createPalyers(names);
        getSocresAndRank(names);
        return 1;
    }

     

  • 相关阅读:
    Codeforces 1045C Hyperspace Highways (看题解) 圆方树
    Codeforces 316E3 线段树 + 斐波那切数列 (看题解)
    Codeforces 803G Periodic RMQ Problem 线段树
    Codeforces 420D Cup Trick 平衡树
    Codeforces 295E Yaroslav and Points 线段树
    Codeforces 196E Opening Portals MST (看题解)
    Codeforces 653F Paper task SA
    Codeforces 542A Place Your Ad Here
    python基础 异常与返回
    mongodb 删除
  • 原文地址:https://www.cnblogs.com/winslam/p/9414720.html
Copyright © 2011-2022 走看看