• Practice3_7_vector_sort_struct_gold_silver_bronze_playerName3


    下面这个就是把结构体中内置的operator单独拿出来,作为比较器实现的方式。

     1 // Practice3_vector_sort_struct.cpp : 定义控制台应用程序的入口点。
     2 //
     3 
     4 #include "stdafx.h"
     5 #include <vector>
     6 #include <algorithm>
     7 #include <iostream>
     8 #include <ctime>
     9 #include <stdio.h>
    10 #include <string>
    11 
    12 using namespace std;
    13 
    14 struct ScoreStruct
    15 {
    16     string name; 
    17     unsigned int gold;
    18     unsigned int silver;
    19     unsigned int bronze;
    20 };
    21 
    22 /* 把结构体中内置的operator单独拿出来,作为比较器实现*/
    23 bool comparatorScoreStruct(const ScoreStruct &ss1, const ScoreStruct &ss2)
    24 {
    25         int temp = ss1.name.compare(ss2.name);
    26         if(ss1.gold != ss2.gold)//首先按照金银铜牌数降序,如果都相等,则按照姓名升序
    27         {
    28             return ss1.gold > ss2.gold;
    29         }
    30 
    31         if(ss1.silver != ss2.silver)
    32         {
    33             return ss1.silver > ss2.silver;
    34         }
    35 
    36         if(ss1.bronze != ss2.bronze)
    37         {
    38             return ss1.bronze > ss2.bronze;
    39         }
    40         
    41         if(temp <0)
    42         {
    43             return 1;
    44         }
    45         return 0;//无论如何,要保证最后要有一个return的
    46 }
    47 
    48 string strs[4] = { "tencent", "google","alibaba", "facebook"};
    49 
    50 void initVector(vector<ScoreStruct> &vec, unsigned int size)
    51 {
    52     srand(unsigned(time(NULL)));
    53     for(unsigned int i =0; i < size; i++)
    54     {
    55         //char buff[32] = {0};
    56         int goldCount = 6;
    57         int silverCount = 6;
    58         int bronzeCount = 6;
    59         //sprintf(buff, "%d", chineseScore);
    60         ScoreStruct ss = {strs[i], goldCount, silverCount, bronzeCount};
    61         /*
    62         ScoreStruct ss = {"0", 0};
    63         strcpy(ss.name, buff);
    64         ss.score = randNum;
    65         */
    66         vec.push_back(ss);
    67     }
    68 }
    69 
    70 void printVector(vector<ScoreStruct> vec)
    71 {
    72     vector<ScoreStruct>::iterator it = vec.begin();
    73     for(; it != vec.end();++it)
    74     {
    75         cout << it->name << "," << it->gold << "," << it->silver << "," << it->bronze << " ";
    76     }
    77     cout<<endl;
    78 }
    79 
    80 int _tmain(int argc, _TCHAR* argv[])
    81 {
    82     vector<ScoreStruct> vect;
    83     initVector(vect, 4);
    84     cout<<"before sort"<<endl;
    85     printVector(vect);
    86     sort(vect.begin(), vect.end(), comparatorScoreStruct);//与上个例子不同的是,多了一个比较器参数
    87     cout<<"after sort"<<endl;
    88     printVector(vect);
    89     return 0;
    90 }

    before sort
    tencent,6,6,6 google,6,6,6 alibaba,6,6,6 facebook,6,6,6
    after sort
    alibaba,6,6,6 facebook,6,6,6 google,6,6,6 tencent,6,6,6

  • 相关阅读:
    在工作中你可能会遇见的事情哈
    原生js判断某个区域的滚动条滚动到了底部
    震惊p div 标签 可以编辑高度随内容的编辑而发生变化
    Codeforces Round #628 (Div. 2) B. CopyCopyCopyCopyCopy(Set)
    Codeforces Round #628 (Div. 2) A. EhAb AnD gCd(LCM & GCD)
    Codeforces Round #613 (Div. 2) C. Fadi and LCM(LCM & GCD)
    Codeforces Round #613 (Div. 2) B. Just Eat It!(前缀和)
    Codeforces Round #613 (Div. 2) A. Mezo Playing Zoma(逻辑)
    GPLT L2-006 树的遍历(二叉树)
    poj 3278 Catch That Cow(记忆化广度优先搜索)
  • 原文地址:https://www.cnblogs.com/liuzc/p/6486492.html
Copyright © 2020-2023  润新知