c++选择排序,按照name先排序  然后 分数 最小的在前

#pragma once
#include <string.h>
#include <iostream>
using namespace std;

class Student
{
public:
    Student(string name,int fen)
    {
        this->name = name;
        this->fen = fen;
    }

    friend ostream& operator<<(ostream& os, Student st) 
    {
        os << st.name<<":" <<st.fen << endl;
        return os;
    }

    //按照name先排序  然后 分数 最小的在前
    bool operator<( Student st) 
    {
        return this->name!=st.name? this->name < st.name : this->fen < st.fen;
    }


private:
    string name;
    int fen;
};

 

------------------------------------------------------------上下是2个文件----------------------------------------------------------------------------------------


#include <iostream>
#include <string.h>
#pragma warning(disable:4996)
//在头部引用这个
#include <iomanip>
#include "Student.h"

using namespace std;


template<typename T>
//选择排序
void SelectSort(T arr[],int n) 
{
    for (int i = 0; i < n; i++)
    {
        //获取最小的索引
        int minIndex = i;
        for (int j = i+1; j < n; j++)
        {
            //如果当前minIndex索引比下一个j索引大 去替换 一直走到最后 找到最小的索引的数据
            //重载operator用的< 这里if 也用<
            if (arr[j]< arr[minIndex])
            {
                minIndex = j;
            }

        }
        swap(arr[i],arr[minIndex]);
        //当前数据和最小索引的数据替换

    }
}

    


int main()
{
    

    
    Student arr[6] = { { "c",8 } ,{ "a",6 }, {"b",6},{"a", 9}, {"c",2} ,{ "b",10 }};
    SelectSort(arr, 6);
    for (int i = 0; i < 6; i++)
    {
        cout<<arr[i] <<endl;
    }

}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值