c++ map的使用方法[转]
1. map构造函数;
map<string , int >mapstring; map<int ,string >mapint;
map<sring, char>mapstring; map< char ,string>mapchar;
map<char ,int>mapchar; map<int ,char >mapint;
2. map添加数据;
map<int ,string> maplive;
1.maplive.insert(pair<int,string>(102,"aclive"));
2.maplive.insert(map<int,string>::value_type(321,"hai"));
3, maplive[112]="April";//map中最简单最常用的插入添加!
3,map中元素的查找:
find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。
map<int ,string >::iterator l_it;;
l_it=maplive.find(112);
if(l_it==maplive.end())
cout<<"we do not find 112"<<endl;
else cout<<"wo find 112"<<endl;
4,map中元素的删除:
如果删除112;
map<int ,string >::iterator l_it;;
l_it=maplive.find(112);
if(l_it==maplive.end())
cout<<"we do not find 112"<<endl;
else maplive.erase(l_it); //delete 112;
5,map中 swap的用法:
Map中的swap不是一个容器中的元素交换,而是两个容器交换;
For example:
#include <map>
#include <iostream>
using namespace std;
int main( )
{
map <int, int> m1, m2, m3;
map <int, int>::iterator m1_Iter;
m1.insert ( pair <int, int> ( 1, 10 ) );
m1.insert ( pair <int, int> ( 2, 20 ) );
m1.insert ( pair <int, int> ( 3, 30 ) );
m2.insert ( pair <int, int> ( 10, 100 ) );
m2.insert ( pair <int, int> ( 20, 200 ) );
m3.insert ( pair <int, int> ( 30, 300 ) );
m1.erase(m1_Iter);//删除一个元素
cout << it->first << endl;
int ii = m1.count(3);
cout << ii << endl;
cout << "The original map m1 is:";
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
cout << " " << m1_Iter->second;
cout << "." << endl;
// This is the member function version of swap
//m2 is said to be the argument map; m1 the target map
m1.swap( m2 );
cout << "After swapping with m2, map m1 is:";
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
cout << " " << m1_Iter -> second;
cout << "." << endl;
cout << "After swapping with m2, map m2 is:";
for ( m1_Iter = m2.begin( ); m1_Iter != m2.end( ); m1_Iter++ )
cout << " " << m1_Iter -> second;
cout << "." << endl;
// This is the specialized template version of swap
swap( m1, m3 );
cout << "After swapping with m3, map m1 is:";
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
cout << " " << m1_Iter -> second;
cout << "." << endl;
}
6.map的sort问题:
Map中的元素是自动按key升序排序,所以不能对map用sort函数:
For example:
#include <map>
#include <iostream>
using namespace std;
int main( )
{
map <int, int> m1;
map <int, int>::iterator m1_Iter;
m1.insert ( pair <int, int> ( 1, 20 ) );
m1.insert ( pair <int, int> ( 4, 40 ) );
m1.insert ( pair <int, int> ( 3, 60 ) );
m1.insert ( pair <int, int> ( 2, 50 ) );
m1.insert ( pair <int, int> ( 6, 40 ) );
m1.insert ( pair <int, int> ( 7, 30 ) );
cout << "The original map m1 is:"<<endl;
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
cout << m1_Iter->first<<" "<<m1_Iter->second<<endl;
}
The original map m1 is:
1 20
2 50
3 60
4 40
6 40
7 30
请按任意键继续. . .
7, map的基本操作函数:
C++ Maps是一种关联式容器,包含“关键字/值”对
begin() 返回指向map头部的迭代器
clear() 删除所有元素
count() 返回指定元素出现的次数
empty() 如果map为空则返回true
end() 返回指向map末尾的迭代器
equal_range() 返回特殊条目的迭代器对
erase() 删除一个元素
find() 查找一个元素
get_allocator() 返回map的配置器
insert() 插入元素
key_comp() 返回比较元素key的函数
lower_bound() 返回键值>=给定元素的第一个位置
max_size() 返回可以容纳的最大元素个数
rbegin() 返回一个指向map尾部的逆向迭代器
rend() 返回一个指向map头部的逆向迭代器
size() 返回map中元素的个数
swap() 交换两个map
upper_bound() 返回键值>给定元素的第一个位置
value_comp() 返回比较元素value的函数
转载于:https://www.cnblogs.com/rooney/archive/2012/07/24/2606485.html
c++ map的使用方法[转]相关推荐
- stream map方法_Java Stream中map和flatMap方法
最近看到一篇讲stream语法的文章,学习Java中map()和flatMap()方法之间的区别. 虽然看起来这两种方法都做同样的事情,都是做的映射操作,但实际上差之毫厘谬以千里. 通过演示Demo中 ...
- 1.7 Java 8中Map新增的方法
Java 8 除了为 Map 增加了 remove(Object key, Object value) 默认方法之外,还增加了如下方法. 名称 说明 Object compute(Object key ...
- ES6新特性之map和reduce方法的使用
map和reduce ES6中,数组新增了map和reduce方法. map map() :接收一个函数,将原数组中的所有元素用这个函数处理后放入新数组返回. 举例:有一个字符串数组,我们希望转为in ...
- select ...as_一起使用.select .map和.reduce方法可充分利用Ruby
select ...as by Declan Meehan 由Declan Meehan 一起使用.select .map和.reduce方法可充分利用Ruby (Get the most out o ...
- go map详细使用方法
go map 比较深入的使用方案 参考blog: https://blog.golang.org/go-maps-in-action 现在基本上所有的编程语言都有自带的map,或者dict,主要提供一 ...
- DevTools 无法加载来源映射无法加载 httplocalhost8082axios.min.map 的解决方法
DevTools 无法加载来源映射:无法加载 http://localhost:8082/axios.min.map 的解决方法 DevTools 无法加载来源映射:无法加载 http://local ...
- Java8新特性——Map的新方法
今天是高考的日子,是大四师兄师姐答辩毕业的日子.一代又来,一代又去.好久没写博客,借此特殊日子整理一下前不久学java8新特性时写的代码,留下痕迹.(本博客的代码根据 java8新特性教程 学习整理, ...
- DevTools 无法加载来源映射:无法加载 http://localhost:8082/axios.min.map 的解决方法
DevTools 无法加载来源映射:无法加载 http://localhost:8082/axios.min.map 的解决方法 DevTools 无法加载来源映射:无法加载 http://local ...
- go二维map_go map详细使用方法
go map 比较深入的使用方案 现在基本上所有的编程语言都有自带的map,或者dict,主要提供一个快速的查找,插入,删除,具备与存储体量无关的O(1)的性能,并且支持key上面的唯一性, 比如ja ...
- Java函数式实现替代策略模式解决 if...else代码,Map+函数式接口方法
之前记录过用自定义注解和策略模式实现发不同消息的功能笔记: 文章地址:发送不同类型的消息----------策略模式_不受天磨非好汉,不遭人妒是庸才--着实着迷゛-CSDN博客r一:首先看下代码结构a ...
最新文章
- Linux 有问必答:如何在Ubuntu或者Debian中启动后进入命令行
- 借花献佛!docker讲解视频
- linux PROC文件系统详解
- JAVASCRIPT常用20种小技巧汇总
- python for循环连续输入五个成绩判断等级_Python条件循环判断
- 国外PHP学习网站书籍资料汇总
- 解决maven打jar包报错:Could not resolve substitution to a value: ${akka.stream.materializer}
- 这个Javascript“要求”是什么?
- 二分查找-----非递归方式
- 计算机文件不能复制到u盘,大文件无法复制到u盘里解决方法
- Laya Shader3D之边缘光照
- webpack 基础学习
- excel取消隐藏_Excel教程:教你两招,批量取消隐藏工作表
- 腾达fh307没有显示服务器名,腾达(Tenda)FH307路由器无线WiFi密码和名称设置 | 192路由网...
- int数组转strpython_python中int与str互转方法
- 这些Android手机现在支持Fortnite(不仅仅是Samsung!)
- Linux登录输入密码后返回登录界面
- linux rm 提示无法删除“xxxxxxx“:不允许的操作
- 常用的固定资产管理软件有哪些
- 解决Win10磁盘活动时间100%,读取速度为0的最终方案