/*
* Copyright (c) 2006 All rights reserved.
* 文件名:MultiMap.cpp
*
* 文件標識:MultiMap
* 摘要:多重映射容器編程示例
* 輸入:無
* 輸出:輸出在程序中插入多重映射容器的信息
*
* 當前版本 0.01
* 作者:羅
* 完成日期:2006年4月3日
*/
#include <iostream>
#include <string>
#include <map>
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;
//創(chuàng)建multimap的實例,整數(shù)(職位編號)映射員工信息
typedef multimap<int, employee> EMPLOYEE_MULTIMAP;
typedef multimap<int, employee>::iterator EMPLOYEE_IT; //隨機訪問迭代器類型
typedef multimap<int, employee>::reverse_iterator EMPLOYEE_RIT; //反向迭代器類型
employee::employee(long eID, string e_Name, float e_Salary)
: ID(eID), name(e_Name), salary(e_Salary) {}
//函數(shù)名:output_multimap
//函數(shù)功能:正向輸出多重映射容器里面的信息
//參數(shù):一個多重映射容器對象
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;
}
}
//函數(shù)名:reverse_output_multimap
//函數(shù)功能:逆向輸出多重映射容器里面的信息
//參數(shù):一個多重映射容器對象
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; //多重映射容器實例
//下面四個語句分別構造一個員工對象插入到多重映射容器
//注意因為是多重映射,所以可以出現(xiàn)重復的鍵,例如下面的信息有兩個職位編號為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);
//輸出容器內(nèi)的記錄條數(shù)
cout<< "共有" << employees.size() << "條員工記錄" << endl;
return 0;
}