• 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

  • 相关阅读:
    刷新验证码
    网页的超链接传递中文参数乱码问题
    Button获取Repeater一行的两个值
    <asp:FileUpload>上传一张图片并且重命名
    <asp:DropDownList数据库读取
    <asp:DropDownList>用法
    <%#Eval(" ")%>用法总结
    DropDownList1 .cs指定初始值
    redolog(未完工)
    cap理论
  • 原文地址:https://www.cnblogs.com/liuzc/p/6486492.html
Copyright © 2020-2023  润新知