STL学习之十:map和multimap用法示例

时间:2022-12-03 20:50:12

本文介绍STL中的map和multimap的用法示例。下面是有关知识点:

1. map 是标准的关联式容器,一个map是一个键值对序列,即(key,value)对,可以快速检索;
2. map中的key是唯一的,集合中的元素按照一定的顺序排列,按一定的规则插入,不能按指定的位置进行插入;
3. map集体的实现采用红黑树变体的平滑二叉树的数据结构,比vector快;
4. map和multimap的区别是:map支持唯一键值,每个键值只能出现一次,而multimap可以出现多次。

下面是具体的代码示例:

1 map

#include "iostream"
using namespace std;
#include "string"
#include "map"

// map元素的添加 遍历 删除 的基本操作
void main91()
{
map<int,string> map1;
//方法 1
map1.insert(pair<int,string>(1,"teacher1"));// 四种插入方式
map1.insert(pair<int,string>(2,"teacher2"));
//方法2
map1.insert(make_pair(3,"teacher3"));
map1.insert(make_pair(4,"teacher4"));
// 方法3
map1.insert(map<int,string>::value_type(5,"teacher5"));
map1.insert(map<int,string>::value_type(6,"teacher6"));
// 方法4
map1[7] = "teacher7";
map1[8] = "teacher8";

// 前三种方法 返回值为pair<iterator,bool> 若key已经存在 则报错
// 方法4 若key已经存在 则修改,覆盖
// 容器的遍历
for (map<int,string>::iterator it = map1.begin();it!=map1.end();it++)
{
cout << it->first << "\t" << it->second << endl;
}
cout << "遍历结束:" << endl;

// 容器的删除
while(!map1.empty())
{
map<int,string>::iterator it = map1.begin();
cout << it->first << "\t" << it->second << endl;
map1.erase(it);
}
}

// map 的查找
void main92()
{
map<int,string> map1;
//方法 1
map1.insert(pair<int,string>(1,"teacher1"));// 四种插入方式
map1.insert(pair<int,string>(2,"teacher2"));
//方法2
map1.insert(make_pair(3,"teacher3"));
map1.insert(make_pair(4,"teacher4"));
// 方法3
map1.insert(map<int,string>::value_type(5,"teacher5"));
map1.insert(map<int,string>::value_type(6,"teacher6"));
// 方法4
map1[7] = "teacher7";
map1[8] = "teacher8";

map<int,string>::iterator it2 = map1.find(100);
if (it2 == map1.end())
{
cout << "没有找到100" << endl;
}
else
{
cout << it2->first << "\t" << it2->second << endl;// it2->first代表map<int,string>::iterator中的第一个变量int , it2->second代表string
}

// map 的 equal_range
pair<map<int,string>::iterator,map<int,string>::iterator> mypair = map1.equal_range(5);// 返回两个迭代器
//第一个迭代器>=5的位置 第二个迭代器>5的位置
if (mypair.first == map1.end())
{
cout << "/第一个迭代器>=5的位置不存在"<< endl;
}
else
{
cout << mypair.first->first << "\t" << mypair.first->second << endl;
}

if (mypair.second == map1.end())
{
cout << " 第二个迭代器>5的位置不存在"<< endl;
}
else
{
cout << mypair.second->first << "\t" << mypair.second->second << endl;
}

}

void main()
{
//main91();
main92();

cout << "hello..."<< endl;

system("pause");

}

2 multimap

 multimap 案例:
 1个key值可以对应多个value = 分组
1.公司有销售部 sale(员工2名)技术研发部development(1)财务部(2)
 2.人员信息有:姓名,年龄,电话,工资等
3.通过multimap进行 信息的插入 保存 显示
4.分部门显示员工信息
下面是代码:
#include "iostream"
using namespace std;
#include "map"
#include "string"

class Person
{
public:
string name;
int age;
string tel;
double sal;

protected:

private:

};
void main101()
{
Person p1,p2,p3,p4,p5;
p1.name = "wang1";
p1.age = 31;
p2.name = "wang2";
p2.age = 32;
p3.name = "zhang1";
p3.age = 33;
p4.name = "zhang2";
p4.age = 34;
p5.name = "liu1";
p5.age = 35;
multimap<string,Person>map2;
map2.insert(make_pair("sale",p1));
map2.insert(make_pair("sale",p2));

map2.insert(make_pair("development",p3));
map2.insert(make_pair("development",p4));

map2.insert(make_pair("finacial",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;;

multimap<string,Person>::iterator it2 = map2.find("development");
int tag = 0;
while(it2 != map2.end() && tag<num2)
{
cout << it2->first << " "<< it2->second.name << " ";
it2 ++;
tag++;
}
cout << endl;

for (multimap<string,Person>::iterator it3=map2.begin();it3!=map2.end();it3++)
{
//cout << it3->first << "\t" << it3->second.name << endl;
if (it3->second.age == 32)
{
it3->second.name = "name32";
}
}
for (multimap<string,Person>::iterator it=map2.begin();it!=map2.end();it++)
{
cout << it->first << "\t" << it->second.name << endl;
}

}
void main()
{
main101();

cout << "hello..."<< endl;

system("pause");

}