免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1668 | 回复: 1
打印 上一主题 下一主题

[C++] C++ 标准模板库(STL)编程示例 - map [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-04-03 10:04 |只看该作者 |倒序浏览
请问我怎么编写适合于映射容器的谓词呢?麻烦大家指点。
我的博客: http://luojiafeng1984.cublog.cn
QQ:344903855
E-MAIL: luojiafeng1984@sina.com

/*
* Copyright (c) 2006 All rights reserved.
* 文件名:Map.cpp
*
* 文件标识:Map
* 摘要:映射容器编程示例
* 输入:无
* 输出:输出在程序中插入映射容器的信息
*
* 当前版本 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;

//创建multimap的实例,整数(职位编号)映射员工信息
typedef map<int, employee> EMPLOYEE_MAP;
typedef map<int, employee>::iterator EMPLOYEE_IT;           //随机访问迭代器类型
typedef map<int, employee>::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;
}

论坛徽章:
0
2 [报告]
发表于 2006-04-03 19:22 |只看该作者
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP