分类:
2012-05-27 03:13:26
//描述:C++ STL 的应用示例
//作者:
//发布日期:2006年3月25日
//文件名:list.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)
{
return (a.salary <= 8000);
}
//ga8000函数是判断工资是否大于8000的谓词
bool ga8000(const employee &a)
{
return (a.salary > 8000);
}
//定义一个元素类型为employee的list容器类型
typedef list
//定义一个list容器的随机访问迭代器
typedef list
//定义一个list容器的反向迭代器
typedef list
//output_list函数正向输出list容器对象内的所有元素
void output_list(EMPLOYEE_LIST employ)
{
EMPLOYEE_IT employit;
for (employit = employ.begin(); employit != employ.end(); employit++)
{
cout << (*employit).ID << '\t' << (*employit).name << '\t' << (*employit).salary << endl;
}
}
//reverse_output_list函数逆向输出list 容器对象内的所有元素
void reverse_output_list(EMPLOYEE_LIST 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_LIST employ; //Create A Empty Queue
EMPLOYEE_IT employit; //Create A Random Access Iterator
//下面的四条语句分别构造一个employee对象,并插入到list容器对象
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));
//正向输出list容器对象的所有元素
output_list(employ);
//按compare谓词删除容器中的元素
employ.remove_if(compare);
output_list(employ);
//逆向输出list容器对象的所有元素
reverse_output_list(employ);
//统计并输出工资高于8000的员工人数
int i = count_if(employ.begin(), employ.end(), ga8000);
cout << "gong zi gao yu 8000 de yuan gong you" << i << "wei!" << endl;
return 0;
}