读书人

Multi地图

发布时间: 2012-09-19 13:43:53 作者: rapoo

Multimap

声明

#include <iostream>#include <string>#include <map>using namespace std;typedef struct employee{//Member Functionpublic: employee(long eID, string e_Name, floate_Salary);//Attributepublic: longID;                 //Employee ID stringname;             //Employee Name floatsalary;          //Employee Salary}employee;//创建multimap的实例,整数(职位编号)映射员工信息typedef multimap<int, employee>EMPLOYEE_MULTIMAP;typedef multimap<int,employee>::iteratorEMPLOYEE_IT;          //随机访问迭代器类型typedef multimap<int,employee>::reverse_iteratorEMPLOYEE_RIT;  //反向迭代器类型employee::employee(long eID, string e_Name, floate_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_MULTIMAPemployees;      //多重映射容器实例    //下面四个语句分别构造一个员工对象插入到多重映射容器 //注意因为是多重映射,所以可以出现重复的键,例如下面的信息有两个职位编号为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;}  

读书人网 >编程

热点推荐