Chinaunix首页 | 论坛 | 博客
  • 博客访问: 486581
  • 博文数量: 77
  • 博客积分: 1047
  • 博客等级: 少尉
  • 技术积分: 898
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-25 17:16
文章分类

全部博文(77)

文章存档

2016年(2)

2013年(2)

2012年(33)

2011年(40)

分类:

2012-05-27 03:12:32

/*
 * Copyright (c) 2006 All rights reserved.
 * 文件名:Map.cpp
 *
 * 文件标识:Map
 * 摘要:映射容器编程示例
 * 输入:无
 * 输出:输出在程序中插入映射容器的信息
 *
 * 当前版本 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 map EMPLOYEE_MAP;
typedef map::iterator EMPLOYEE_IT;           //随机访问迭代器类型
typedef map::reverse_iterator EMPLOYEE_RIT;  //反向迭代器类型

employee::employee(long eID, string e_Name, float e_Salary)
         : ID(eID), name(e_Name), salary(e_Salary) {}
 
//函数名:output_map
//函数功能:正向输出映射容器里面的信息
//参数:一个映射容器对象
void output_map(EMPLOYEE_MAP 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_map
//函数功能:逆向输出映射容器里面的信息
//参数:一个映射容器对象
void reverse_output_map(EMPLOYEE_MAP 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_MAP employees;       //映射容器实例
     //下面四个语句分别构造一个员工对象插入到映射容器
  //注意映射容器不可以插入键相同的元素,下面的信息有两个职位编号为111的员工
  //第二条记录将不会被插入到映射容器
     employees.insert(EMPLOYEE_MAP::value_type(118, employee(100, "luojiafeng", 8000)));
  employees.insert(EMPLOYEE_MAP::value_type(118, employee(109, "jiafeng", 8000)));
  employees.insert(EMPLOYEE_MAP::value_type(112, employee(101, "luojiahui", 6000)));
  employees.insert(EMPLOYEE_MAP::value_type(113, employee(102, "luokaifeng", 10000)));
  employees.insert(EMPLOYEE_MAP::value_type(116, employee(103, "xujinghua", 20000)));
  //正序输出映射容器中的信息
  cout << "职位编号" << "员工ID" << '\t'
   << "姓名" << '\t' << '\t' << "工资" << endl;
  output_map(employees);
  //逆序输出映射容器中的信息
  cout << "职位编号" << "员工ID" << '\t'
   << "姓名" << '\t' << '\t' << "工资" << endl;
  reverse_output_map(employees);
  //输出容器内的记录条数
  cout<< "共有" << employees.size() << "条员工记录" << endl;
     return 0;
}  
阅读(1334) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~