1 map插入访问
#include<iostream>
#include<map>
using namespace std;
class Person {
public:
friend void test1();
friend void printMap(map<int,Person>&m);
Person() {}
Person(int num, string name, float score) {
this->num = num;
this->name = name;
this->score = score;
}
private:
int num;
string name;
float score;
};
void printMap(map<int, Person>&m) {
map<int, Person>::const_iterator it = m.begin();
for (; it != m.end(); it++)
cout << (*it).first << " "<<(*it).second.num<<" " << (*it).second.name << " " << (*it).second.score << endl;
}
void test1() {
map<int, Person> m;
//方式1
m.insert(pair<int,Person>(1,Person(101,"andy ",86.6f)));
//方式2(推荐)
m.insert(make_pair(5,Person(105,"lily ",85.6f)));
//方式3
m.insert(map<int,Person>::value_type(2,Person(102,"tom ",78.9f)));
//方式4(危险)
m[4] = Person(104,"jiery",87.2f);
printMap(m);
}
int main() {
test1();
return 0;
}
方式4,存在键值是可以访问的;;但是访问不存在的键值,会将不存在的键加入map ;
//方式4,存在键值是可以访问的;;但是访问不存在的键值,会将不存在的键加入map
cout << m[4].num << " " << m[4].name << " " << m[4].score << endl;
cout << m[9].num << " " << m[9].name << " " << m[9].score << endl;
cout << "--------------------------" << endl;
//从新打印map,发现m[9]已经加入了
printMap(m);
map不允许有相同的键值,,但可以有相同的实值
void test2() {
map<int, Person> m;
//map不允许有相同的键值,,但可以有相同的实值
m.insert(make_pair(5, Person(105, "lily ", 85.6f)));
m.insert(make_pair(5, Person(108, "lily ", 85.6f)));
m.insert(make_pair(6, Person(105, "lily ", 85.6f)));
m.insert(make_pair(7, Person(105, "lily ", 85.6f)));
printMap(m);
}
2 multimap 案例
公司今天招聘了 5 个员工,5 名员工进入公司之后,需要指派员工在那个部门;
工作人员信息有: 姓名 年 龄 电话 工资等组成
通过 Multimap 进行信息的插入 保存显示 分部门显示员工信息 显示全部员工信息。
#define SALE_DEPATMENT 1 //销售部门
#define DEVELOP_DEPATMENT 2 //研发部门
#define FINACIAL_DEPATMENT 3 //财务部门
#define ALL_DEPATMENT 4 //所有部门
完整代码
#include<iostream>
#include<map>
#include<vector>
#include<time.h>
#include<string>
using namespace std;
//创建员工类
class Employee {
public:
friend void printMulimap(multimap<int, Employee>& m);
friend void createEmployee(vector<Employee>& v);
friend void employeeJoinDepartment(vector<Employee>& v, multimap<int, Employee>& m);
friend void showDepartmentEmployee(multimap<int, Employee>& m);
Employee() {}
Employee(string name,int age, string tel, int salary) {
this->name = name;
this->age = age;
this->tel = tel;
this->salary = salary;
}
private:
string name;
int age;
string tel;
int salary;
};
void printMulimap(multimap<int, Employee>& m) {
multimap<int, Employee>::const_iterator it = m.begin();
for (; it != m.end(); it++)
cout << (*it).first << " " << (*it).second.name << " " << (*it).second.age << " " << (*it).second.tel <<" "<<(*it).second.salary<<endl;
}
//创建5名员工,存放在vector
void createEmployee(vector<Employee>&v) {
//Employee p1("zhang", 30," 123456789", 5000);
//Employee p2("zhao ", 26, "123456789", 15000);
//Employee p3("wang ", 28, "123456789", 8000);
//Employee p4("liu ", 26, "123456789", 12000);
//Employee p5("sun ", 27, "123456789", 9000);
//v.push_back(p1);
//v.push_back(p2);
//v.push_back(p3);
//v.push_back(p4);
//v.push_back(p5);
//设置随机数种子
srand(time(NULL));
for (int i = 0; i < 5; i++) {
string seedName = "ABCDE";
string tmpName = "员工";
tmpName += seedName[i];
int age = 20 + i;
int salary = 15000 + rand() % 10 * 1000;
string tel = to_string(rand());
v.push_back(Employee(tmpName,age,tel,salary));
}
}
//5名员工加入部门
void employeeJoinDepartment(vector<Employee>& v, multimap<int, Employee> &m) {
//m.insert(make_pair(1, v[0]));
//m.insert(make_pair(1, v[1]));
//m.insert(make_pair(2, v[2]));
//m.insert(make_pair(2, v[3]));
//m.insert(make_pair(3, v[4]));
vector<Employee>::iterator it = v.begin();
for (; it != v.end(); it++) {
//*it = Employee
cout << "请输入 【" <<(*it).name<<"】加入的部门:1 销售 2 研发 3 财务 "<< endl;
int op;
cin >> op;
m.insert(make_pair(op,*it));
}
}
//显示部门员工
void showDepartmentEmployee(multimap<int, Employee>& m) {
cout << "请输入部门:\n1 销售\n2 研发\n3 财务\n4 全部" << endl;
int op;
cin >> op;
switch (op) {
case 1:
cout << "销售部门员工信息如下:" << endl;
break;
case 2:
cout << "研发部门员工信息如下:" << endl;
break;
case 3:
cout << "财务部门员工信息如下:" << endl;
break;
case 4:
cout << "所有部门员工信息如下:" << endl;
printMulimap(m);
break;
}
//寻找op的位置
multimap<int, Employee>::iterator ret;
ret = m.find(op);
if (ret == m.end()) return;
//统计op的个数
int count = m.count(op);
//从op的位置 按照个数 逐个变量
for (int i= 0; i < count; i++, ret++) {
cout << (*ret).first << " " << (*ret).second.name << " " << (*ret).second.age << " " << (*ret).second.tel << " " << (*ret).second.salary << endl;
}
}
//接口测试
void test1() {
//创建5名员工,存放在vector
vector<Employee> v;
createEmployee(v);
//5名员工加入部门
multimap<int, Employee> m;
employeeJoinDepartment(v,m);
//显示部门员工
showDepartmentEmployee(m);
}
int main(){
test1();
return 0;
}