/*
* Copyright (c) 2006 All rights reserved.
* 文件名:PriorityQueue.cpp
*
* 文件标识:PriorityQueue
* 摘要:容器适配器PriorityQueue编程示例
* 输入:无
* 输出:输出各个队列对象的信息
*
* 当前版本 0.01
* 作者:罗
* 完成日期:2006年4月5日
*/
#include
#include
#include
#include
#include
#include
using namespace std;
struct employee
{
//Member Function
public:
employee() {} //默认构造函数
employee(long eID, string e_Name, float e_Salary);
//Attribute
public:
long ID; //Employee ID
string name; //Employee Name
float salary; //Employee Salary
};
//员工类构造函数
employee::employee(long eID, string e_Name, float e_Salary)
: ID(eID), name(e_Name), salary(e_Salary) {}
class KeyComp
{
public:
bool operator() (const employee& A, const employee& B)
{
return (A.salary < B.salary);
}
};
//定义一个元素类型为employee、用Deque容器维护被控序列的队列类型
typedef priority_queue >, KeyComp > V_EMPLOYEE_QUEUE;
//定义一个元素类型为employee、用List容器维护被控序列的队列类型
typedef priority_queue >, KeyComp > D_EMPLOYEE_QUEUE;
int main(int argc, char* argv[])
{
V_EMPLOYEE_QUEUE v_employee; //声明一个容器对象为Vector的优先队列对象
D_EMPLOYEE_QUEUE d_employee; //声明一个容器对象为Deque的优先队列对象
employee *temp; //声明一个employee对象指针
//下面的三条语句分别构造三个employee对象,然后入容器对象为Deque的队列
v_employee.push(V_EMPLOYEE_QUEUE::value_type(100, "huahua", 20000));
v_employee.push(V_EMPLOYEE_QUEUE::value_type(101, "jiafeng", 8000));
v_employee.push(V_EMPLOYEE_QUEUE::value_type(102, "guangli", 10000));
//输出容器对象为Vector的优先队列的第一个employee元素,也就是优先级最高的元素,也就是工资最高的那个元素
//动态空间申请,并用优先队列中优先级最高的元素初始化
assert(!v_employee.empty());
temp = new employee(v_employee.top());
cout << "工资最高的员工为:" << endl;
cout << temp->ID << '\t' << temp->name << '\t' << temp->salary << endl;
delete temp;
//下面的二条语句分别构造二个employee对象,然后入容器对象为List的队列
d_employee.push(D_EMPLOYEE_QUEUE::value_type(100, "huahua", 20000));
d_employee.push(D_EMPLOYEE_QUEUE::value_type(101, "jiafeng", 8000));
assert(!d_employee.empty());
d_employee.pop();
cout << "容器对象为Vector的优先队列是否为空? " << (v_employee.empty() ? "TRUE" : "FALSE") << endl;
cout << "容器对象为Vector的优先队列共有" << v_employee.size() << "个employee对象!" << endl;
cout << "容器对象为Deque的优先队列是否为空?" << (d_employee.empty() ? "TRUE" : "FALSE") << endl;
cout << "容器对象为Deque的优先队列共有" << d_employee.size() << "个employee对象!" << endl;
return 0;
}
|
文件: |
priorityqueue.rar |
大小: |
1KB |
下载: |
下载 | |
阅读(3506) | 评论(0) | 转发(1) |