STL之六:map/multimap用法详解
转载于:http://blog.csdn.net/longshengguoji/article/details/8547007
map/multimap
使用map/multimap之前要加入头文件#include<map>,map和multimap将key/value当作元素,进行管理。它们可根据key的排序准则自动将元素排序。multimap允许重复元素,map不允许重复元素。
map和multimap内部的数据结构也是平衡二叉树。
map和multimap根据元素的key自动对元素进行排序,要修改元素的key必须先删除拥有该key的元素,然后插入拥有新的key/value的元素。
常用函数
1.构造函数和析构函数
map m:创建空映射,不包含任何元素
map m(op):以op为排序准则,产生一个空的map
map m1(m2):复制c2中的元素到c1中
map m(const value_type *first, const value_type* last):复制[first, last)之间元素构成新映射
map m(const value_type *first, const value_type* last,op):以op为排序准则,复制[first, last)之间元素构成新映射。
m.~set()销毁所有元素,释放内存
multimap mm:创建空映射,不包含任何元素
multimap mm(op):以op为排序准则,产生一个空的multimap
multimap m1(m2):复制m2中的元素到m1中
multimap m(const value_type *first, const value_type* last):复制[first, last)之间元素构成新映射
multimap m(const value_type *first, const value_type* last,op):以op为排序准则,复制[first, last)之间元素构成新映射
m.~multimap()销毁所有元素,释放内存
#include "stdafx.h"
#include <iostream>
#include <map> using namespace std; bool fncomp (char lhs, char rhs) {return lhs<rhs;} struct classcomp {
bool operator() (const char& lhs, const char& rhs) const
{return lhs<rhs;}
}; int main ()
{
map<char,int> first; first['a']=10;
first['b']=30;
first['c']=50;
first['d']=70; map<char,int> second (first.begin(),first.end()); map<char,int> third (second); map<char,int,classcomp> fourth; // class as Compare bool(*fn_pt)(char,char) = fncomp;
map<char,int,bool(*)(char,char)> fifth (fn_pt); // function pointer as Compare return 0;
}
2.大小、判断空函数
int size() const:返回容器元素个数
bool empty() const:判断容器是否空,若返回true,表明容器已空。
3.增加删除函数
iterator insert(const value_type& x):插入元素x
Map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(, “student_one”));
Map<int, string> mapStudent;
mapStudent.insert(map<int, string>::value_type (, “student_one”));
iterator insert(iterator it,const value_type& x):在迭代指针it处插入元素x
void insert(const value_type *first,const value_type* last):插入[first, last)之间元素
iterator erase(iterator it):删除迭代指针it处元素
iterator erase(iterator first,iterator last):删除[first, last)之间元素
size_type erase(const Key& key):删除键值等于key的元素
#include "stdafx.h"
#include <iostream>
#include <map> using namespace std; int main ()
{
map<char,int> mymap; mymap.insert(pair<char,int>('a',10));
mymap.insert(pair<char,int>('z',200)); pair<map<char,int>::iterator,bool> ret;
ret = mymap.insert(pair<char,int>('z',500));
if (ret.second == false)
{
cout<<"element 'z' already existed";
cout<<"with a value of "<<ret.first->second<<'\n';
} map<char,int>::iterator it = mymap.begin();
mymap.insert(it,pair<char,int>('b',300));
mymap.insert(it,pair<char,int>('c',400)); map<char,int> anothermap;
anothermap.insert(mymap.begin(),mymap.find('c')); cout<<"mymap contains :\n";
for (it = mymap.begin();it!= mymap.end();it++)
{
cout<<it->first<<"=>"<<it->second<<'\n';
} cout<<"anothermap contains :\n";
for (it = anothermap.begin();it!= anothermap.end();it++)
{
cout<<it->first<<"=>"<<it->second<<'\n';
}
return 0;
}
上述代码运行结果为
#include "stdafx.h"
#include <iostream>
#include <map> using namespace std; int main ()
{
map<char,int> mymap;
map<char,int>::iterator it; mymap['a'] = 10;
mymap['b'] = 20;
mymap['c'] = 30;
mymap['d'] = 40;
mymap['e'] = 50;
mymap.insert(pair<char,int>('f',60)); cout<<"initial mymap contains :\n";
for (it = mymap.begin();it!= mymap.end();it++)
{
cout<<it->first<<"=>"<<it->second<<'\n';
} it = mymap.find('b');
mymap.erase(it); mymap.erase('c'); it = mymap.find('e');
mymap.erase(it,mymap.end()); cout<<"now mymap contains :\n";
for (it = mymap.begin();it!= mymap.end();it++)
{
cout<<it->first<<"=>"<<it->second<<'\n';
} return 0;
}
上述代码运行结果为:

如果想往map/multimap中修改一个映射的值,应先插入一个新映射,再把与修改的映射删除。
4.遍历函数
iterator begin():返回首元素的迭代器指针
iterator end():返回尾元素的迭代器指针
reverse_iterator rbegin():返回尾元素的逆向迭代器指针
reverse_iterator rend():返回首元素前一个位置的迭代器指针
5.操作函数
const_iterator lower_bound(const Key& key):返回键值大于等于key的迭代器指针
const_iterator upper_bound(const Key& key):返回键值大于key的迭代器指针
int count(const Key& key) const:返回键值等于key的元素的个数
pair<const_iterator,const_iterator> equal_range(const Key& key) const:返回容器中键值等于key的迭代指针[first, last)
const_iterator find(const Key& key) const:查找功能,返回键值等于key的迭代器指针
void swap(set& s):交换但映射元素
void swap(multiset& s):交换多映射元素
6.特殊函数
reference operator[](const Key& k):仅在但映射map类中,可以以数组的形式给映射添加键-值对,并可返回值的引用。
STL之六:map/multimap用法详解的更多相关文章
- STL:map/multimap用法详解
map/multimap 使用map/multimap之前要加入头文件#include<map>,map和multimap将key/value当作元素,进行管理.它们可根据key的排序准则 ...
- STL map 常见用法详解
<算法笔记>学习笔记 map 常见用法详解 map翻译为映射,也是常用的STL容器 map可以将任何基本类型(包括STL容器)映射到任何基本类型(包括STL容器) 1. map 的定义 / ...
- js数组中foEach和map的用法详解 jq中的$.each和$.map
数组中foEach和map的用法详解 相同点: 1.都是循环遍历数组(仅仅是数组)中的每一项. 2.forEach() 和 map() 里面每一次执行匿名函数都支持3个参数:数组中的当前项value, ...
- 2.9 C++STL map/multimap容器详解
文章目录 2.9.1 引入 2.9.2 代码示例 map案列 multimap案列 2.9.3 代码运行结果 总结 2.9.1 引入 map相对于set区别,map具有键值和实值,所有元素根据键值自动 ...
- C++标准模板库(STL)——map常见用法详解
map的定义 map<typename1, typename2> mp; map需要确定映射前类型和映射后类型,所以需要在<>内填写两个类型,第一个是键的类型,第二个是值的类型 ...
- [转]Java中Map的用法详解
转载地址:http://www.zhixing123.cn/jsp/30113.html Map简介 将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值.此接口取代 Dictio ...
- Java中Map的用法详解
Map简介 将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值.此接口取代 Dictionary 类,后者完全是一个抽象类,而不是一个接口. Map 接口提供三种collecti ...
- RxJava(二) map操作符用法详解
欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/51531348 本文出自:[余志强的博客] 1 map操作符的作用 R ...
- C语言 · C++中map的用法详解
转载自:http://blog.csdn.net/sunquana/article/details/12576729 一.定义 (1) map<string, int> Map ...
随机推荐
- CF245H Queries for Number of Palindromes
题目描述 给你一个字符串s由小写字母组成,有q组询问,每组询问给你两个数,l和r,问在字符串区间l到r的字串中,包含多少回文串. 时空限制 5000ms,256MB 输入格式 第1行,给出s,s的长度 ...
- matlab画图:设置y轴位置,使y轴在x轴的中间
sigmoid函数图像 x=-10:0.1:10; y=sigmf(x,[1 0]); plot(x,y) 画出的图像如下所示: 怎么将Y轴放在中间呢,而不是在左边? 即如何得到这种效果呢? 方法 ...
- 158. Valid Anagram【LintCode by java】
Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification ...
- 【springmvc+mybatis项目实战】杰信商贸-4.maven依赖+PO对+映射文件
上一篇我们附件的增删改查功能全部完成.但是我们的附件有一个字段叫做“类型”(ctype),这里我们要使用数据字典,所以对于这一块我们要进行修改. 首先介绍一下数据字典 数据字典它是一个通用结构,跟业务 ...
- ionic ios样式偏移解决方案。
在css属性内增加: .item-ios [item-end] { //解决ios系统上尾部图标出现重影而增加的格式. margin: 0px -15.3px 0px 0px; margin-bott ...
- 十二:NodeManager
NM负责启动和管理节点上的containers.AM通过containers来运行任务. Health Checker Service 创建检查服务 NM运行一个检查服务来检查节点的状态,该服 ...
- [leetcode-658-Find K Closest Elements]
Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...
- 自定义View 和 ViewGroup
一. 自定义View介绍 自定义View时, 继承View基类, 并实现其中的一些方法. (1) ~ (2) 方法与构造相关 (3) ~ (5) 方法与组件大小位置相关 (6) ~ (9) 方法与触摸 ...
- C#操作Excel执行分类多条件汇总合并
之前发了一片模拟合并,详见模拟Excel同一列相同值的单元格合并 在之前的文章中介绍了思想,其中Excel采用的二维数组模拟,今天花了点时间,学习了一下C#操作Excel,实现了类似的效果! 准备 需 ...
- Spring中Controller和RequestMapping的详解
先看一个简单的实例: @Controller @RequestMapping("/hello") public class anyTypeController{ @RequestM ...