STL - miltimap(可重映射)
2015-07-08 21:57
369 查看
#include <iostream> #include <map> #include <string> using namespace std; //Multimap 案例: //1个key值可以对应多个valude =分组 //公司有销售部 sale (员工2名)、技术研发部 development (1人)、财务部 Financial (2人) //人员信息有:姓名,年龄,电话、工资等组成 //通过 multimap进行 信息的插入、保存、显示 //分部门显示员工信息 class Person { public: string name; int age; string tel; double saly; }; void multimapInit() { Person p1, p2, p3, p4, p5; p1.name = "王1"; p1.age = 31; p2.name = "王2"; p2.age = 32; p3.name = "张3"; p3.age = 33; p4.name = "张4"; p4.age = 34; p5.name = "赵5"; p5.age = 35; multimap<string, Person> map2; //sale部门 map2.insert(make_pair("sale", p1)); map2.insert(make_pair("sale", p2)); //development 部门 map2.insert(make_pair("development", p3)); map2.insert(make_pair("development", p4)); //Financial 部门 map2.insert(make_pair("Financial", p5)); for (multimap<string, Person>::iterator it = map2.begin(); it != map2.end(); it++) { cout << it->first << "\t" << it->second.name << endl; } cout << "遍历结束" << endl; // int num2 = map2.count("development"); cout << "development部门人数==>" << num2 << endl; cout << "development部门员工信息" << endl; multimap<string, Person>::iterator it2 = map2.find("development"); int tag = 0; while (it2 != map2.end() && tag < num2) { cout << it2->first << "\t" << it2->second.name << endl; it2++; tag++; } } //age = 32修改成 name32 void multimapChange() { Person p1, p2, p3, p4, p5; p1.name = "王1"; p1.age = 31; p2.name = "王2"; p2.age = 32; p3.name = "张3"; p3.age = 33; p4.name = "张4"; p4.age = 34; p5.name = "赵5"; p5.age = 35; multimap<string, Person> map2; //sale部门 map2.insert(make_pair("sale", p1)); map2.insert(make_pair("sale", p2)); //development 部门 map2.insert(make_pair("development", p3)); map2.insert(make_pair("development", p4)); //Financial 部门 map2.insert(make_pair("Financial", p5)); cout << "\n按照条件 检索数据 进行修改 " << endl; for (multimap<string, Person>::iterator it = map2.begin(); it != map2.end(); it++) { //cout << it->first << "\t" << it->second.name << endl; if (it->second.age == 32) { it->second.name = "name32"; } } for (multimap<string, Person>::iterator it = map2.begin(); it != map2.end(); it++) { cout << it->first << "\t" << it->second.name << endl; } } int main() { multimapInit(); multimapChange(); return 0; }
相关文章推荐
- GIT: 第二步:提交项目至GitHub
- 【MySQL】MySql定时更改数据库字段
- STL - miltimap(可重映射)
- 基于Angular+WebAPI+OData的增删改查
- 3Sum
- Atmega48-88-168熔丝位设置方法
- c++primer第十章笔记
- IE浏览器开启对JavaScript脚本的支持
- ASP.NET MVC Boilerplate简介
- 牛人看过来
- checkBox复选框,获得选中那一行所有列的数据
- 隐私策略
- [cocos2dx] cocosdx编译工程那些事
- ThinkPHP BASE
- linux快速生成大文件方法
- [LeetCode]Regular Expression Matching(Unsolved)
- Object-c中的点语法
- mysql通过DATE_FORMAT将错误数据恢复
- LeetCode之“树”:Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II
- 算法复习之选择排序