C 标准模板库(STL)编程示例

来源:百度文库 编辑:神马文学网 时间:2024/04/19 12:45:47
C++ 标准模板库(STL)编程示例 - multimap /*
* Copyright (c) 2006 All rights reserved.
* 文件名:MultiMap.cpp
*
* 文件标识:MultiMap
* 摘要:多重映射容器编程示例
* 输入:无
* 输出:输出在程序中插入多重映射容器的信息
*
* 当前版本 0.01
* 作者:罗
* 完成日期:2006年4月3日
*/
#include
#include
#include
using namespace std;
typedef 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;
//创建multimap的实例,整数(职位编号)映射员工信息
typedef multimap EMPLOYEE_MULTIMAP;
typedef multimap::iterator EMPLOYEE_IT;           //随机访问迭代器类型
typedef multimap::reverse_iterator EMPLOYEE_RIT;  //反向迭代器类型
employee::employee(long eID, string e_Name, float e_Salary)
: ID(eID), name(e_Name), salary(e_Salary) {}
//函数名:output_multimap
//函数功能:正向输出多重映射容器里面的信息
//参数:一个多重映射容器对象
void output_multimap(EMPLOYEE_MULTIMAP employ)
{
EMPLOYEE_IT employit;
for (employit = employ.begin(); employit != employ.end(); employit++)
{
cout << (*employit).first << '\t' << (*employit).second.ID
<< '\t' << (*employit).second.name << '\t' << (*employit).second.salary
<< '\t' << endl;
}
}
//函数名:reverse_output_multimap
//函数功能:逆向输出多重映射容器里面的信息
//参数:一个多重映射容器对象
void reverse_output_multimap(EMPLOYEE_MULTIMAP employ)
{
EMPLOYEE_RIT employit;
for (employit = employ.rbegin(); employit != employ.rend(); employit++)
{
cout << (*employit).first << '\t' << (*employit).second.ID
<< '\t' << (*employit).second.name << '\t' << (*employit).second.salary
<< '\t' << endl;
}
}
int main(int argc, char *argv[])
{
EMPLOYEE_MULTIMAP employees;       //多重映射容器实例
//下面四个语句分别构造一个员工对象插入到多重映射容器
//注意因为是多重映射,所以可以出现重复的键,例如下面的信息有两个职位编号为118的员工
employees.insert(EMPLOYEE_MULTIMAP::value_type(118, employee(100, "luojiafeng", 8000)));
employees.insert(EMPLOYEE_MULTIMAP::value_type(112, employee(101, "luojiahui", 6000)));
employees.insert(EMPLOYEE_MULTIMAP::value_type(113, employee(102, "luokaifeng", 10000)));
employees.insert(EMPLOYEE_MULTIMAP::value_type(118, employee(103, "xujinghua", 20000)));
//正序输出多重映射容器中的信息
cout << "职位编号" << "员工ID" << '\t'
<< "姓名" << '\t' << '\t' << "工资" << endl;
output_multimap(employees);
//逆序输出多重映射容器中的信息
cout << "职位编号" << "员工ID" << '\t'
<< "姓名" << '\t' << '\t' << "工资" << endl;
reverse_output_multimap(employees);
//输出容器内的记录条数
cout<< "共有" << employees.size() << "条员工记录" << endl;
return 0;
}