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. java中 Math和StrictMath

    今天无意中看到java api中有StrictMath 这个工具类,发现它部分调用实现是用了Math中的实现.Math 这个类API 1.0版本就有了,StrictMath API是1.3版本才出来的 ...

  2. scala中的Option

    Scala中Option是用来表示一个可选类型 什么是可选? --> 主要是指 有值(Some) 和 无值(None)-->Some和None是Option的子类 val myMap:Ma ...

  3. EF连接mysql,出现A call to SSPI failed错误,解决办法

    我的使用场景是用EF连接AWS的mysql RDS,会偶发性的出现A call to SSPI failed错误, System.AggregateException: One or more err ...

  4. C++初探

    //string1.cpp #include <iostream> int main() { using namespace std; ]={'d','o','g'}//这是char数组, ...

  5. 华为方舟编译器 下载 和 LiteOS Studio Setup 2019-04-16.exe SDK下载

    华为方舟编译器是首个取代Android虚拟机模式的静态编译器,可供开发者在开发环境中一次性将高级语言编译为机器码.此外,方舟编译器未来将支持多语言统一编译,可大幅提高开发效率. 编译器下载 [Ark] ...

  6. rabbitMQ基础应用

    1.安装erlang [root@localhost ~]#yum -y install erlang 2.安装rabbitMQ [root@localhost ~]#yum -y install r ...

  7. Caml 多表关联查询

    using (SPSite site = new SPSite(SiteUrl)) { using (SPWeb web = site.RootWeb) { SPQuery query = new S ...

  8. Android中webview html5 自动播放本地视频

    MainActivity代码 public class Html5VideoAutoPlay extends Activity { WebView webview = null; @Override ...

  9. Canny算法检测边缘

    Canny算法是边缘检测的一个经典算法,比单纯用一些微分算子来检测的效果要好很多,其优势有以下几点: 边缘误检与漏检率低. 边缘定位准确,且边界较细. 自带一定的滤噪功能,或者说,对噪声的敏感度要比单 ...

  10. 【异常】The dependencies of some of the beans in the application context form a cycle

    一.异常出现场景以及异常信息 场景:SpringBoot中自定义DataSource数据源 异常信息: -- :: --- [ main] o.s.b.d.LoggingFailureAnalysis ...