C++队列和优先权队列的使用---应用:带时限作业排序

C++队列和优先权队列的使用—应用:带时限作业排序


C++队列的使用

首先包含头文件

include

队列可以用线性表(list)或双向队列(deque)来实现(注意vector container 不能用来实现queue,因为vector 没有成员函数pop_front!):
queue<list<int>> q1;
queue<deque<int>> q2;
其成员函数有“判空(empty)” 、“尺寸(Size)” 、“首元(front)” 、“尾元(backt)” 、“加入队列(push)” 、“弹出队列(pop)”等操作。

1.back() 返回一个引用,指向最后一个元素
2.empty() 如果队列空则返回真
3.front() 返回第一个元素
4.pop() 删除第一个元素
5.push() 在末尾加入一个元素
6.size() 返回队列中元素的个数

例如,队列在带时限作业排序中的应用

#include<iostream>
`#include<queue>`
using namespace std;
struct Node         //状态空间树结点结构
{
    Node(Node *par, int k, int num)
    {
        parent = par; j = k; NodeNum = num;
    }
    Node *parent;    //指向该结点的双亲结点
    int j;           //该结点代表的解分量x[i]=j
    int NodeNum;     //该结点的编号,增加的变量,可以不要
};

template<class T>
struct qNode       //活结点表中的活结点结构
{
    qNode(){}
    qNode(T p, T los, int sd, int k, Node *pt)
    {
        prof = p; loss = los; d = sd; ptr = pt; j = k;
    }
    T prof, loss;   //当前结点X的下界函数c(X)=loss,上界函数u(X)=24-prof
    int j, d;        //当前活结点所代表的分量x[j]=j, d是迄今为止的时间
    Node *ptr;      //指向状态空间树中相应的结点
};

template<class T>
class JS
{
public:
    JS(T *prof, int *de, int *time, int size);
    T JSFIFOBB();                              //构造状态空间树,求最优解值
    void GenerateAns(int *x, int &k);          //一维数组x为最优解向量,k中返回x的分量解
private:
    T *p, total;                                //p为收益数组,total初始为n个作业收益之和
    int *t, *d, n;                             //t为作业处理时间数组,d为按非减次序排列的作业时间数组
    Node *ans, *root;                           //root指向状态空间树的根,ans指向最优解答案结点
};

template<class T>
JS<T>::JS(T *prof, int *de, int *time, int size)
{
    n = size;
    p = new T[n];  d = new int[n];   t = new int[n];
    total = 0;
    for (int i = 0; i<n; i++)
    {
        p[i] = prof[i];   total += p[i];
        d[i] = de[i];     t[i] = time[i];
    }
}

`#define mSize 20`

template<class T>
T JS<T>::JSFIFOBB()
{
    Node *E, *child;

    queue< qNode<T> > q; //生成一个FIFO队列实例q

    int num = 1;                     //结点编号num
    E = root = new Node(NULL, -1, num);  //构造状态空间树的根结点root
    cout << " root->NodeNum=" &
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
C++中,我们可以通过自定义比较函数来实现自定义优先队列排序。比较函数是一个返回布尔值的函数,用于确定元素的优先级。在优先队列中,元素按照默认的排序规则进行排序,即大根堆或小根堆。默认情况下,元素按照从大到小的顺序排列,也就是大根堆。如果要实现不同的排序规则,我们可以自定义比较函数来改变元素的排序顺序。 下面是一个示例代码,展示了如何在C++中自定义优先队列排序: ```cpp #include <iostream> #include <queue> using namespace std; // 自定义比较函数,实现从小到大的排序规则 struct Compare { bool operator() (int a, int b) { return a > b; } }; void custom_priority_queue_sort() { int source_data = {3, 5, 8, 1, 10, 2, 9, 15, 13, 16}; priority_queue<int, vector<int>, Compare> q; // 使用自定义的比较函数 for (auto n : source_data) q.push(n); while (!q.empty()) { cout << q.top() << endl; q.pop(); } } int main() { custom_priority_queue_sort(); return 0; } ``` 在上述代码中,我们定义了一个名为`Compare`的结构体,其中重载了小括号运算符`()`。在自定义的比较函数中,我们将元素按照从小到大的顺序排列,即返回`a > b`。然后在创建优先队列时,我们通过指定自定义比较函数`Compare`来改变排序规则。 运行以上代码,输出结果为: ``` 1 2 3 5 8 9 10 13 15 16 ``` 以上代码演示了如何在C++中自定义优先队列排序规则。通过自定义比较函数,我们可以实现各种不同的排序方式来满足特定需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值