STL常用容器用法之——Map和multimap
来源:互联网 发布:海森矩阵的作用 编辑:程序博客网 时间:2024/05/23 19:48
map/multimap的简介
map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对。它提供基于key的快速检索能力。
map中key值是唯一的。集合中的元素按一定的顺序排列。元素插入过程是按排序规则插入,所以不能指定插入位置。
map的具体实现采用红黑树变体的平衡二叉树的数据结构。在插入操作和删除操作上比vector快。
map可以直接存取key所对应的value,支持[]操作符,如map[key]=value。
multimap与map的区别:map支持唯一键值,每个键只能出现一次;而multimap中相同键可以出现多次。multimap不支持[]操作符。
#include <map>
1、map元素的添加/遍历/删除基本操作
map<int, string> map1;//方法1 通过pair的方式插入对象
map1.insert(pair<int, string>(1,"teacher01") );
map1.insert(pair<int, string>(2,"teacher02") );
//方法2 通过pair的方式插入对象
map1.insert(make_pair(3, "teacher04") );
map1.insert(make_pair(4, "teacher05") );
//方法3 通过value_type的方式插入对象
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";
前三种方法,采用的是insert()方法,该方法返回值为pair<iterator,bool>
第四种方法非常直观,但存在一个性能的问题。插入3时,先在mapStu中查找主键为3的项,若没发现,则将一个键为3,值为初始化值的对组插入到mapStu中,然后再将值修改成“小刘”。若发现已存在3这个键,则修改这个键对应的value。
string strName = mapStu[2]; //取操作或插入操作
只有当mapStu存在2这个键时才是正确的取操作,否则会自动插入一个实例,键为2,值为初始化值。
//方法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, "teacher04") );
map1.insert(make_pair(4, "teacher05") );
//方法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 << "\t" << mypair5.first->second <<endl;
}
//容器的遍历
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); //删除pos迭代器所指的元素,返回下一个元素的迭代器。
}
2、map的查找
map.find(key); 查找键key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回map.end();
map.count(keyElem); //返回容器中key为keyElem的对组个数。对map来说,要么是0,要么是1。对multimap来说,值可能大于1。
map.lower_bound(keyElem); //返回第一个key>=keyElem元素的迭代器。
map.upper_bound(keyElem); // 返回第一个key>keyElem元素的迭代器。
map.equal_range(keyElem); //返回容器中key与keyElem相等的上下限的两个迭代器。上限是闭区间,下限是开区间,如[beg,end)。
例:
//map的查找 //异常处理
map<int, string>::iterator it2 = map1.find(100);
if (it2 == map1.end())
{
cout << "key 100 的值 不存在" << endl;
}
else
{
cout << it2->first << "\t" << it2->second << endl;
}
//equal_range //异常处理
pair<map<int, string>::iterator , map<int, string>::iterator> mypair = map1.equal_range(5); //返回两个迭代器 形成一个 pair
//第一个迭代器 >= 5的 位置
//第一个迭代器 = 5的 位置
if (mypair.first == map1.end() )
{
cout << "第一个迭代器 >= 5的 位置 不存在" << endl;
}
else
{
cout << mypair.first->first << "\t" << mypair.first->second << endl; //5 teacher05
}
//使用第二个迭代器
if (mypair.second == map1.end() )
{
cout << "第二个迭代器 > 5的 位置 不存在" << endl;
}
else
{
cout << mypair.second->first << "\t" << mypair.second->second << endl; //6 teacher06
}
3、Multimap
Multimap 案例:
//1个key值可以对应多个valude =è分组
//公司有销售部 sale (员工2名)、技术研发部 development (1人)、财务部 Financial (2人)
//人员信息有:姓名,年龄,电话、工资等组成
//通过 multimap进行 信息的插入、保存、显示
//分部门显示员工信息
{
public:
string name;
int age;
string tel;
double saly;
};
{
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 << "development部门人数==>" << num2 << endl; //2
cout << "development部门员工信息" << endl;
multimap<string, Person>::iterator it2 = map2.find("development");
int tag = 0;
while (it2 != map2.end() && tag < num2) // it2 != map2.end()判断的是整个容器是否结束,需要一个tag
{
cout << it2->first << "\t" << it2->second.name << endl;
it2 ++;
tag ++;
}
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;
}
}
- STL常用容器用法之——Map和multimap
- 33STL之Map和multimap容器
- STL之Map和multimap容器
- STL之map和multimap容器
- STL之Map和multimap容器
- C++ STL之map和multimap用法
- STL容器之map/multimap
- STL学习笔记— —容器map和multimap
- STL学习笔记8— —容器map和multimap
- STL-map和multimap容器
- STL map和multimap型容器常用的成员
- 今日学习札记——STL常用容器:vector、list、set和multiset、map和multimap(11.17)
- STL 笔记(二) 关联容器 map、set、multimap 和 multimap常用函数
- STL容器:map,multimap
- STL之map&multimap容器(笔记)
- STL学习之十:map和multimap用法示例
- STL::map,multimap用法
- STL之六:map/multimap用法详解
- 宏定义求数组元素个数
- 数据结构--左偏树(可并堆)
- 关于C语言中的内存覆盖问题分析
- SingletonBeanRegistry
- 【windows】【编译器版本查】教你查看VS IDE中编译器版本号
- STL常用容器用法之——Map和multimap
- 爬取智联招聘(面向对象)
- 对数学建模论文的一些建议
- relative 和absolute
- springboot整合Mybatis
- vertical-align应用
- hdu 6180 Schedule 贪心
- python的官方下载, 安装, IDE工具介绍,HelloWord
- [js高手之路]原型式继承与寄生式继承