c++运算符重载

C/C++相关内容转载
该博客为转载内容,转载自https://www.cnblogs.com/cstdio1/p/10933340.html ,涉及C/C++相关知识。
#include <iostream>
#include <string>
#include <list>
using namespace std;

class Student {
   string name;
   char sex;
   int score;
   string grade;

public:
   Student(string name, char sex, int score, string grade);
   friend ostream &operator<< (ostream& os, Student st) ;
   friend bool operator<(Student &st1, Student &st2);    
};
//你提交的代码将被嵌入到这里
ostream &operator<< (ostream& os, Student st){
os<<st.name<<" "<<st.sex<<" "<<st.score<<" "<<st.grade<<'\n';
return os;    
}
bool operator<(Student &st1, Student &st2){
if(st1.score>=st2.score) return false;
return true;    
}
Student::Student(string name, char sex, int score, string grade) {
   this->name = name;
   this->sex = sex;
   this->score = score;
   this->grade = grade;
}

int main() {
   list<Student> st;
   string name, grade;
   char sex;      int score;
    
   for(int i = 0; i < 5; i++) {
      cin>>name;      cin>>sex;
      cin>>score;       cin>>grade;
      st.push_back(Student(name, sex, score, grade));
   }

   st.sort();

   list<Student>::iterator p = st.begin();
   while(p != st.end()) {
      cout<<*p;
      p++;
   }
   return 0;
}

 

转载于:https://www.cnblogs.com/cstdio1/p/10933340.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值