The outputs:
No. : 2
Deadline : 1
Cost : 7
No. : 1
Deadline : 3
Cost : 6
No. : 3
Deadline : 5
Cost : 2
The codes:
// how to finish the assignments in the lowest cost.
# include <iostream>
# include <iomanip>
# include <algorithm>
# include <list>
using namespace std;
struct assignment_details{
int No;
int Deadline; // to illustrate the deadlines of every assignment
int Cost; // the cost of miss the deadline of every assignment
bool operator< (const assignment_details& other) const {
if (Cost == other.Cost)
return Deadline < other.Deadline; // if the cost of the assignment are same, sort them by increaing
else
return Cost > other.Cost; // if the cost of the assignment are different, sort them by decreaing
}
assignment_details(int no, int deadline, int cost){
No = no; Deadline = deadline; Cost = cost;
}
};
ostream& operator<< (ostream& COUT, assignment_details& assigment){
COUT << "No. : " << assigment.No << endl;
COUT << "Deadline : " << assigment.Deadline << endl;
COUT << "Cost : " << assigment.Cost << endl;
return COUT;
}
int main()
{
list<assignment_details> assignments;
assignment_details assignment_0(1, 3, 6);
assignments.push_back(assignment_0);
assignment_details assignment_1(2, 1, 7);
assignments.push_back(assignment_1);
assignment_details assignment_2(3, 5, 2);
assignments.push_back(assignment_2);
assignments.sort();
for(list<assignment_details>::iterator it = assignments.begin(); it != assignments.end(); ++it){
cout << *it <<endl;
}
return 0;
}
reference link:
C++ list(STL list)容器完全攻略(超级详细)
https://www.youtube.com/watch?v=BnMnozsSPmw
the second reference is to tell you how to undersatand and use the operator overloading