Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <functional>
- #include <iomanip>
- #include <iostream>
- #include <string>
- #include <list>
- struct Student
- {
- friend std::istream& operator >>(std::istream& input, Student& stu)
- {
- return input >> stu.number >> stu.name >> stu.chinese_score >> stu.math_score >> stu.english_score;
- }
- friend std::ostream& operator <<(std::ostream& output, const Student& stu)
- {
- output << std::setw(15) << stu.number
- << std::setw(15) << stu.name
- << std::setw(5) << stu.chinese_score
- << std::setw(5) << stu.math_score
- << std::setw(5) << stu.english_score;
- return output;
- }
- std::string number;
- std::string name;
- int chinese_score;
- int math_score;
- int english_score;
- };
- template <typename T, typename Fn = std::less<T> >
- struct Compare: std::binary_function<Student, Student, bool>
- {
- Compare(const T Student::*_p, Fn _fn=Fn()): p(_p), fn(_fn) {}
- bool operator ()(const Student& a, const Student& b) const
- {
- return fn(a.*p, b.*p);
- }
- private:
- const T Student::*p;
- Fn fn;
- };
- int main()
- {
- std::list<Student> student_list;
- student_list.sort(Compare<std::string>(&Student::number)); //std::less<std::string>
- student_list.sort(Compare<int, std::greater<int> >(&Student::chinese_score));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement