Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些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> ( , ) );
m1.insert ( pair <int, int> ( , ) );
m1.insert ( pair <int, int> ( , ) );
m2.insert ( pair <int, int> ( , ) );
m2.insert ( pair <int, int> ( , ) );
m3.insert ( pair <int, int> ( , ) );
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> ( , ) );
m1.insert ( pair <int, int> ( , ) );
m1.insert ( pair <int, int> ( , ) );
m1.insert ( pair <int, int> ( , ) );
m1.insert ( pair <int, int> ( , ) );
m1.insert ( pair <int, int> ( , ) );
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的函数

//转载自:Live的博客

稍作修改,便于阅读。

map函数怎么用咧↓↓↓的更多相关文章

  1. map 函数----filter函数

    # map 函数 l = (1,2,4,5,6,7,8,9,) print(list(map(lambda x:x**2,l)))#使用list类型((map函数(lambda 匿名函数定义x值:x* ...

  2. 实现python中的map函数

    假设Python没有提供map()函数,自行编写my_map()函数实现与map()相同的功能.以下代码在Python 2.7.8中实现. 实现代码: def my_map(fun,num): i = ...

  3. js parseInt和map函数

    今天看了一个js的题目["1","2","3"].map(parseInt),看到后脑海中浮现的答案是[1,2,3],但是看到正确答案后蒙了 ...

  4. JavaScript中map函数和filter的简单举例(转)

    js的数组迭代器函数map和filter,可以遍历数组时产生新的数组,和python的map函数很类似1)filter是满足条件的留下,是对原数组的过滤:2)map则是对原数组的加工,映射成一一映射的 ...

  5. Python-lambda函数,map函数,filter函数

    lambda函数主要理解: lambda 参数:操作(参数). lambda语句中,冒号前是参数,可以有多个,用逗号隔开,冒号右边的返回值.lambda语句构建的其实是一个函数对象 map函数: ma ...

  6. map() 函数

    map()函数 map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回. 例如,对于li ...

  7. STL : map函数的运用 --- hdu 4941 : Magical Forest

    Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  8. Swift之map函数的强大之处

    CollectionType Map 在CollectionType的extension中map方法的定义: extension CollectionType { /// Return an `Arr ...

  9. python map函数

    map()函数 map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回. 例如,对于li ...

随机推荐

  1. 【More Effective C++ 条款1】仔细区别pointers和references

    1)操作符的差别 指针使用"*"和"->"操作符,而引用使用"."操作符 2)初始化的差别 有空指针,但是没有空引用,和const对象 ...

  2. Shiro 使用 JWT Token 配置类参考

    项目中使用了 Shiro 进行验证和授权,下面是 Shiro 配置类给予参考. 后来并没有使用 Shiro,感觉使用 JWT 还是自己写拦截器比较灵活,使用 Shiro 后各种地方需要魔改,虽然功能也 ...

  3. 奥展项目笔记01--不同网站,点击工具--开发人员工具F12,显示的页面怎么不一样

    开发人员工具F12,显示的页面不一样: 样式1: 样式2: 解决方案:兼容模式和极速模式的开发者工具不一样,改成极速模式就ok了.

  4. CMake的含义和用法解读

    什么是 CMake 你或许听过好几种 Make 工具,例如 GNU Make ,QT 的 qmake ,微软的 MS nmake,BSD Make(pmake),Makepp,等等.这些 Make 工 ...

  5. Blend 硬货 绑定

    原文:Blend 硬货 绑定 开始讲一点 硬技能 怎么用Blend实现绑定 效果 详细说一下绑定 1)default 2)OneTime 3) One Way 4)TwoWay 5) OneWayto ...

  6. table布局 常见问题总结

    table实用属性: 属性 值 作用 描述 table-layout auto 自动计算列宽 对table和td.th指定的宽度无效 浏览器会计算所有单元格的内容宽度才能得出一列宽度 (默认值) fi ...

  7. C#循环 — break VS continue

    一.简介 1.break语句:循环-循环中断并停止,退出当前循环: 流程图: 2.continue:循环-循环下一次迭代继续执行. 流程图: 执行过程:立即结果本次循环,判断循环条件,如果成立,则进入 ...

  8. 使用Docker之镜像的拉取、查询、删除

    1:查看镜像列表 2:拉取镜像    通过命令可以从镜像仓库中拉取镜像,默认从Docker Hub 获取. 命令格式: docker image pull <repository>:< ...

  9. vue接入腾讯防水墙代码

    vue接入腾讯防水墙代码 开始创建代码: 登陆调用方法代码

  10. 开发工具--搭建python环境

    工具|搭建python环境 实现python2版本与python3版本的环境搭建. 正文 1.Python下载 官网: www.python.org 下载: ( 64位3.5.2Windows x86 ...