栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

How to sort your new data structure by using embedded sort function in list

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

How to sort your new data structure by using embedded sort function in list

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 
# include 
# include 
# include 
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 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::iterator it = assignments.begin(); it != assignments.end(); ++it){
        cout << *it < 

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 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/346884.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号