map 存放元素和遍历
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <map>
#include <string>
using namespace std;
void map_operate()
{
map<int, string> map1;
// 方法1
map1.insert(pair<int, string>(1, "teacher01"));
map1.insert(pair<int, string>(2, "teacher02"));
// 方法2
map1.insert(make_pair(3, "teacher03"));
map1.insert(make_pair(4, "teacher04"));
// 方法3
map1.insert(map<int, string>::value_type(5, "teacher05"));
map1.insert(map<int, string>::value_type(6, "teacher06"));
// 方法4
map1[7] = "teacher07";
map1[8] = "teacher08";
// 容器遍历
for (map<int, string>::iterator it = map1.begin(); it != map1.end(); it++)
{
cout << it->first << "\t" << it->second << endl;
}
cout << "遍历结束" << endl;
}
int main()
{
map_operate();
system("pause");
return 0;
}
上述四种插入方法的异同:
前三种方法返回的是pair<iterator, bool>
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <map>
#include <string>
using namespace std;
void map_operate()
{
map<int, string> map1;
// 方法1
pair<map<int, string>::iterator, bool> mypair1 = map1.insert(pair<int, string>(1, "teacher01"));
map1.insert(pair<int, string>(2, "teacher02"));
// 方法2
pair<map<int, string>::iterator, bool> mypair3 = map1.insert(make_pair(3, "teacher03"));
map1.insert(make_pair(4, "teacher04"));
// 方法3
pair<map<int, string>::iterator, bool> mypair5 = map1.insert(map<int, string>::value_type(5, "teacher05"));
if (mypair5.second != true)
{
cout << "key 5 插入失败" << endl;
}
else
{
cout << mypair5.first->first << " : " << mypair5.first->second<< endl;
}
// 插入2个5 会插入失败
pair<map<int, string>::iterator, bool> mypair6 = map1.insert(map<int, string>::value_type(5, "teacher06"));
if (mypair6.second != true)
{
cout << "key 5 插入失败" << endl;
}
else
{
cout << mypair6.first->first << " : " << mypair6.first->second << endl;
}
// 方法4 相同的 key 会覆盖
map1[7] = "teacher07";
map1[7] = "teacher77";
// 容器遍历
for (map<int, string>::iterator it = map1.begin(); it != map1.end(); it++)
{
cout << it->first << "\t" << it->second << endl;
}
cout << "遍历结束" << endl;
}
int main()
{
map_operate();
system("pause");
return 0;
}
前三种方法返回的是pair<iterator, bool> 若 key存在,那么会报错 方法四若key已经存在,则会修改(覆盖)
multimap 的使用案例
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <map>
#include <string>
using namespace std;
/*
* 一个key可以对应多个value = 分组
* 通过multimap进行信息的插入/保存/显示
* 分部门显示员工信息
*/
class Person
{
public:
string name;
int age;
string tel;
double sal;
};
void multimapDemo()
{
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> map1;
// sale 部门
map1.insert(make_pair("sale", p1));
map1.insert(make_pair("sale", p2));
// development
map1.insert(make_pair("development", p3));
map1.insert(make_pair("development", p4));
// Financial
map1.insert(make_pair("financial", p5));
for (multimap<string, Person>::iterator it = map1.begin(); it != map1.end(); it++)
{
cout << it->first << "\t" << it->second.name << endl;
}
cout << "遍历结束" << endl;
// 按部门显示员工信息
int count = map1.count("development");
cout << "development 部门人数 : " << count << endl;
cout << "development 部门员工信息 : " << endl;
multimap<string, Person>::iterator it2 = map1.find("development");
int tag = 0;
while (it2 != map1.end() && tag < count)
{
cout << it2->first << "\t" << it2->second.name << endl;
it2++;
tag++;
}
}
// 修改容器的内容 把 age = 32 的name 修改成为name32
void multimapDemo_02()
{
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> map1;
// sale 部门
map1.insert(make_pair("sale", p1));
map1.insert(make_pair("sale", p2));
// development
map1.insert(make_pair("development", p3));
map1.insert(make_pair("development", p4));
// Financial
map1.insert(make_pair("financial", p5));
cout << "按照条件检索数据进行修改 : " << endl;
for (multimap<string, Person>::iterator it = map1.begin(); it != map1.end(); it++)
{
if (it->second.age == 32)
{
it->second.name = "name32";
}
cout << it->first << "\t" << it->second.name << endl;
}
cout << "遍历结束" << endl;
}
int main()
{
// multimapDemo();
multimapDemo_02();
system("pause");
return 0;
}