首页 > 编程语言 > 详细

c++运算符重载

时间:2019-05-27 21:25:58      阅读:130      评论:0      收藏:0      [点我收藏+]
#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;
}

 

c++运算符重载

原文:https://www.cnblogs.com/cstdio1/p/10933340.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!