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的sort问题:
Map中的元素是自动按key升序排序,所以不能对map用sort函数

6.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的函数

//使用map的Demo

 void CLoadDllDemoDlg::OnBnClickedButton17()
{//STL MAP
/*定义CString为key,int值为value的map*/
std::map<CString, int>mapDemo; for(int i = ; i < ; i++)
{//添加数据
CString strKey;
strKey.Format(_T("key:%d"), i);
/*方法1*/
mapDemo.insert(std::make_pair<CString, int>(strKey, i));
/*方法2*/
//mapDemo.insert(map<CString, int>::value_type (strKey, i));
} /*查找数据*/
CString strFindKey;
std::map<CString, int>::iterator itFind;
for(int p = ; p < ; p++)
{
strFindKey.Format(_T("key:%d"), p);
itFind = mapDemo.find(strFindKey);
if(itFind != mapDemo.end())
{//找到数据
int nVal = itFind->second;
if( == nVal)
{//删除该条数据
mapDemo.erase(itFind);
}
}
}
}

C++ STL map使用的更多相关文章

  1. stl::map之const函数访问

    如何在const成员数中访问stl::map呢?例如如下代码: string ConfigFileManager::MapQueryItem(const string& name) const ...

  2. hdu4941 Magical Forest (stl map)

    2014多校7最水的题   Magical Forest Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit ...

  3. [CareerCup] 13.2 Compare Hash Table and STL Map 比较哈希表和Map

    13.2 Compare and contrast a hash table and an STL map. How is a hash table implemented? If the numbe ...

  4. STL MAP及字典树在关键字统计中的性能分析

    转载请注明出处:http://blog.csdn.net/mxway/article/details/21321541 在搜索引擎在通常会对关键字出现的次数进行统计,这篇文章分析下使用C++ STL中 ...

  5. POJ 3096 Surprising Strings(STL map string set vector)

    题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...

  6. STL MAP 反序迭代

    ITS_NOTICE_MAP::reverse_iterator it = noticeMap.rbegin(); for ( ; it != noticeMap.rend(); ++it ) { I ...

  7. 泛型Binary Search Tree实现,And和STL map比较的经营业绩

    问题叙述性说明: 1.binary search tree它是一种二进制树的.对于key值.比当前节点左孩子少大于右子. 2.binary search tree不是自平衡树.所以,当插入数据不是非常 ...

  8. Dictionary,hashtable, stl:map有什么异同?

    相同点:字典和map都是泛型,而hashtable不是泛型. 不同点:三者算法都不相同 Hashtable,看名字能想到,它是采用传统的哈希算法:探测散列算法,而字典则采用的是散列拉链算法,效率较高, ...

  9. STL Map和multimap 容器

    STL Map和multimap 容器 map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供 基于key的快速检索能力.       ...

随机推荐

  1. 目标跟踪之meanshift---均值漂移搞起2000过时的

    基于灰度均值分布的目标跟踪! http://blog.csdn.net/wds555/article/details/24499599 但他有些有点: 1.不会受遮挡太多影响 Mean Shift跟踪 ...

  2. poj 1730Perfect Pth Powers(分解质因数)

                                                             id=1730">Perfect Pth Powers Time Li ...

  3. HDU 5336 XYZ and Drops 2015 Multi-University Training Contest 4 1010

    这题的题意是给你一幅图,图里面有水滴.每一个水滴都有质量,然后再给你一个起点,他会在一開始的时候向四周发射4个小水滴,假设小水滴撞上水滴,那么他们会融合,假设质量大于4了,那么就会爆炸,向四周射出质量 ...

  4. C#特性类的使用

    特性类的使用过程: 第一步:定义一个特性类,定义一些成员来包含验证时需要的数据:第二步:创建特性类实例:创建一个特性类的实例,里面包含着验证某一个属性或者字段需要的数据.将该实例关联到某个属性上面.第 ...

  5. 如何设置SVN提交时必须输入注释

    在Windows环境 在SVN的Repositories路径,E:\Repositories\demo20170408\hooks: 创建pre-commit.bat批处理文件. 文件内容: @ech ...

  6. dialog更新数据

    将数据显示在最上面

  7. go 客户端服务端通信

    client.go package main import ( "bufio" "encoding/json" "fmt" "ha ...

  8. Python对象拷贝——深拷贝与浅拷贝

    对象赋值 浅拷贝 深拷贝 1. 对象赋值 对象的赋值实际上是对对象的引用.也就是说当把一个对象赋值给另一个对象时,只是拷贝了引用.如: >>> t1 = tuple('furzoom ...

  9. 侧方位停车想一次过,掌握边线30cm很重要!

    侧方位停车要想一次过关,关键在于保持车身距离库边线30cm左右的距离.但是,往往有很多学员掌控不好这个距离,导致倒库时压线.那么,如何找准这个30cm呢?下面,小编就来教大家方法,赶紧学习吧! 侧方位 ...

  10. CentOS中文乱码之解决办法

    在学习Linux的过程中,最先碰到的是通过SSH终端连接时发现有乱码出现,使用这篇文章先从这里说起. 在 ssh , telnet 终端中文显示乱码解决办法#vim /etc/sysconfig/i1 ...