#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;
}
}