map的key值不可以重复
multimap的key值可以重复
#if 1
#include<iostream>
#include<map>
using namespace std;
//初始化
void test01() {//map容器参数 第一个参数key的类型 第二个参数value类型map<int, int> mymap;//插入元素 pair.first key值 pair.second value值//第一种初始化方法pair<map<int,int>::iterator,bool> ret=mymap.insert(pair<int, int>(10, 20)); //前三种都是这样:判断插入成功与否if (ret.second) {cout << "第一次插入成功" << endl;}elsecout << "第一次插入失败" << endl;ret = mymap.insert(pair<int, int>(10, 30));if (ret.second) {cout << "第二次插入成功" << endl;}elsecout << "第二次插入失败" << endl;//第二种初始化方法mymap.insert(make_pair(20, 20));//第三种初始化方法mymap.insert(map<int,int>::value_type(30, 30));//第四种初始化方法mymap[40] = 40;for (map<int,int>::iterator it=mymap.begin();it!=mymap.end();it++){//*it取出来的是一个paircout <<"key:" <<it->first << " value:"<<(*it).second << endl;}//此种方法//发现如果key不存在,创建pair插入到map容器中//如果发现key存在,那么会用新的value值替换原value值mymap[40] = 10;cout << "第二次遍历" << endl;for (map<int, int>::iterator it = mymap.begin(); it != mymap.end(); it++){//*it取出来的是一个paircout << "key:" << it->first << " value:" << (*it).second << endl;}//如果通过【】方式访问map中一个不存在的key,//那么map会将这个访问的key插入到map中,并且给value一个默认值cout << mymap[60] << endl;cout << "第三次遍历" << endl;for (map<int, int>::iterator it = mymap.begin(); it != mymap.end(); it++){//*it取出来的是一个paircout << "key:" << it->first << " value:" << (*it).second << endl;}
}
class MyKey {
public:MyKey(int index, int id) {this->mIndex = index;this->mID = id;}
public:int mIndex;int mID;
};
struct mycompare {bool operator()(MyKey k1, MyKey k2) {return k1.mIndex > k2.mIndex;}
};void test02() {//注意此处添加比较函数(系统不会对自定数据类型进行比较)map<MyKey, int,mycompare> mymap; mymap.insert(make_pair(MyKey(1, 2), 1));mymap.insert(make_pair(MyKey(2, 3), 2));mymap.insert(make_pair(MyKey(4, 5), 4));for (map<MyKey, int, mycompare>::iterator it = mymap.begin(); it != mymap.end(); it++) {cout << it->first.mIndex << " " << (*it).first.mID << endl;}}
//equal_range
void test03() {map<int, int> mymap;mymap.insert(make_pair(1, 1));mymap.insert(make_pair(2, 2));mymap.insert(make_pair(3, 3));mymap.insert(make_pair(4, 4));mymap.insert(make_pair(6, 6));pair<map<int, int>::iterator, map<int, int>::iterator> ret=mymap.equal_range(5);cout << (*(ret.first)).first << endl;cout << (*(ret.second)).first << endl;ret = mymap.equal_range(2);cout << (*(ret.first)).first << endl;cout << (*(ret.second)).first << endl;}int main() {cout << "test01" << endl;test01();cout << "test02" << endl;test02();cout << "test03" << endl;test03();return 0;
}
#endif
运行结果:
multimap使用案例:
我的试验:
#if 1
#include<iostream>
#include<map>
#include<time.h>
#include<vector>
#include<string>
#define SaleDepartment 1
#define FinanceDepartment 2
#define TechnologyDepartment 3
using namespace std;//multimap案例
//公司招聘5名员工 需要指派员工在哪个部门工作
//人员信息有:姓名 年龄 电话 工资等
//通过multimap 进行信息的插入 保存 显示
//分部门显示员工信息 显示全部员工信息
class Person {
public:Person(){}Person(string name, int age, string tel, int salary) {mName = name;mAge = age;mTel = tel;mSalary = salary;}
public:string mName;int mAge;string mTel;int mSalary;
};
void CreateEmployee(vector<Person>& v) {string Name="ABCDE";for (int i = 0; i < 5; i++){Person p;p.mName = "员工";p.mName+=Name[i];p.mAge = 20 + i;p.mTel = "010-88888888";p.mSalary = 10000;v.push_back(p);}
}
void AssignDepartment(multimap<int, Person> &mp, vector<Person>& v) {srand(time(NULL));for (int i = 0; i < v.size(); i++){int DepartmentID = rand() % 3 + 1; //生成1-3之间的随机数cout << "rand() value:"<<DepartmentID << endl;switch (DepartmentID){case 1://mp.insert(SaleDepartment, v[i]); 我的错误//mp.insert(make_pair<SaleDepartment, v[i]>); 我的错误cout << "enter SaleDepartment" << endl;mp.insert(make_pair(SaleDepartment, v[i]));break;case 2:cout << "enter FinanceDepartment" << endl;mp.insert(make_pair(FinanceDepartment, v[i]));break;case 3:cout << "enter TechnologyDepartment" << endl;mp.insert(make_pair(TechnologyDepartment, v[i]));break;default:break;}}}void ShowEmployeeByDepart(multimap<int, Person> &mp, int Department) {int n = mp.count(Department);cout << Department << endl;for (multimap<int, Person>::iterator it = mp.find(Department); it != mp.end(), n>0; it++){cout <<(*it).second.mName << endl;n--;}
}
void ShowEmployee(multimap<int, Person> &mp) {ShowEmployeeByDepart(mp, SaleDepartment);ShowEmployeeByDepart(mp, FinanceDepartment);ShowEmployeeByDepart(mp, TechnologyDepartment);}void test01() {vector<Person> vp;CreateEmployee(vp);multimap<int, Person> mp;AssignDepartment(mp, vp);ShowEmployee(mp);
}int main() {test01();return 0;
}
#endif
标准:
#if 1
#include<iostream>
#include<map>
#include<time.h>
#include<vector>
#include<string>
#define SaleDepartment 1
#define FinanceDepartment 2
#define TechnologyDepartment 3
using namespace std;//multimap案例
//公司招聘5名员工 需要指派员工在哪个部门工作
//人员信息有:姓名 年龄 电话 工资等
//通过multimap 进行信息的插入 保存 显示
//分部门显示员工信息 显示全部员工信息//class 默认private struct默认public
class Person {
public:Person() {}Person(string name, int age, string tel, int salary) {mName = name;mAge = age;mTel = tel;mSalary = salary;}
public:string mName;int mAge;string mTel;int mSalary;
};void CreateEmployee(vector<Person>& v) {string Name = "ABCDE";for (int i = 0; i < 5; i++){Person p;p.mName = "员工";p.mName += Name[i]; //string类型拼接不可直接用+号,必须用+=p.mAge = 20 + i;p.mTel = "010-88888888";p.mSalary = 10000;v.push_back(p);}
}void AssignDepartment(multimap<int, Person> &mp, vector<Person>& v) {srand(time(NULL));for (vector<Person>::iterator it=v.begin();it!=v.end();it++){int DepartmentID = rand() % 3 + 1; //生成1-3之间的随机数 rand()%(max-min+1)+mincout << "rand() value:" << DepartmentID << endl;switch (DepartmentID){case SaleDepartment://mp.insert(SaleDepartment, v[i]); 我的错误//mp.insert(make_pair<SaleDepartment, v[i]>); 我的错误//cout << "enter SaleDepartment" << endl;mp.insert(make_pair(SaleDepartment, *it));break;case FinanceDepartment://cout << "enter FinanceDepartment" << endl;mp.insert(make_pair(FinanceDepartment, *it));break;case TechnologyDepartment://cout << "enter TechnologyDepartment" << endl;mp.insert(make_pair(TechnologyDepartment, *it));break;default:break;}}}void ShowEmployeeByDepart(multimap<int, Person> &mp, int Department) {//查找此department下的第一个员工multimap<int, Person>::iterator itt = mp.find(Department);//count查找到此Department下的员工个数int n = mp.count(Department); cout << Department << endl;for (multimap<int, Person>::iterator it=itt; it != mp.end(), n>0; it++){cout <<"姓名:" <<(*it).second.mName <<"年龄:"<<(*it).second.mAge<<"电话:"<<(*it).second.mTel<< "工资:" << (*it).second.mSalary << endl;n--;}
}
void ShowEmployee(multimap<int, Person> &mp) {ShowEmployeeByDepart(mp, SaleDepartment);ShowEmployeeByDepart(mp, FinanceDepartment);ShowEmployeeByDepart(mp, TechnologyDepartment);}void test01() {//存放新员工的信息vector<Person> vp;//创建员工CreateEmployee(vp);multimap<int, Person> mp;//员工分组AssignDepartment(mp, vp);//打印每一部门员工信息ShowEmployee(mp);
}int main() {test01();return 0;
}
#endif