STL demo

#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; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值