//描述:C++ STL 的应用示例
//作者:
//发布日期:2006年3月25日
//文件名:deque.cpp
#include
#include
#include
#include
using namespace std;
struct employee
{
//Member Function
public:
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::employee(long eID, string e_Name, float e_Salary) : ID(eID), name(e_Name), salary(e_Salary) {}
//compare函数是对vector容器中存储的员工资料按工资进行排序的谓词
bool compare(const employee &a, const employee &b)
{
return (a.salary > b.salary);
}
//ga8000函数是判断工资是否大于8000的谓词
bool ga8000(const employee &a)
{
return (a.salary > 8000);
}
//定义一个元素类型为employee的deque类型
typedef deque EMPLOYEE_DEQUE;
//定义一个deque类型的随机访问迭代器类型
typedef deque::iterator EMPLOYEE_IT;
//定义一个deque类型的反向迭代器
typedef deque::reverse_iterator REVERSE_EMPLOYEE_IT;
//output_deque函数正向输出deque的所有元素
void output_deque(EMPLOYEE_DEQUE employ)
{
EMPLOYEE_IT employit;
for (employit = employ.begin(); employit != employ.end(); employit++)
{
cout << (*employit).ID << '\t' << (*employit).name << '\t' << (*employit).salary << endl;
}
}
//reverse_output_deque函数反向输出deque的所有元素
void reverse_output_deque(EMPLOYEE_DEQUE employ)
{
REVERSE_EMPLOYEE_IT employit;
for (employit = employ.rbegin(); employit != employ.rend(); employit++)
{
cout << (*employit).ID << '\t' << (*employit).name << '\t' << (*employit).salary << endl;
}
}
int main(int argc, char* argv[])
{
EMPLOYEE_DEQUE employ; //Create A Empty Queue
EMPLOYEE_IT employit; //Create A Random Access Iterator
//下面四条语句分别构造四个employee对象,并加入到双端队列
employ.push_back(employee(100, "luojiafeng", 10000));
employ.push_back(employee(101, "liujie", 8000));
employ.push_back(employee(102, "zhangshan", 8800));
employ.push_front(employee(108, "wujiang", 5000));
employ.erase(employ.begin()); //删除队列的首元素
//按compare谓词对deque容器对象employ进行排序
sort(employ.begin(), employ.end(), compare);
//正向输出deque容器对象employ的所有元素
output_deque(employ);
//逆向输出deque窗口对象employ的所有元素
reverse_output_deque(employ);
//统计并输出工资高于8000的人数
int i = count_if(employ.begin(), employ.end(), ga8000);
cout << "gong zi gao yu 8000 de you " << i << "wei!" << endl;
return 0;
}
阅读(2280) | 评论(0) | 转发(1) |