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

  1. void CLoadDllDemoDlg::OnBnClickedButton17()
  2. {//STL MAP
  3. /*定义CString为key,int值为value的map*/
  4. std::map<CString, int>mapDemo;
  5.  
  6. for(int i = ; i < ; i++)
  7. {//添加数据
  8. CString strKey;
  9. strKey.Format(_T("key:%d"), i);
  10. /*方法1*/
  11. mapDemo.insert(std::make_pair<CString, int>(strKey, i));
  12. /*方法2*/
  13. //mapDemo.insert(map<CString, int>::value_type (strKey, i));
  14. }
  15.  
  16. /*查找数据*/
  17. CString strFindKey;
  18. std::map<CString, int>::iterator itFind;
  19. for(int p = ; p < ; p++)
  20. {
  21. strFindKey.Format(_T("key:%d"), p);
  22. itFind = mapDemo.find(strFindKey);
  23. if(itFind != mapDemo.end())
  24. {//找到数据
  25. int nVal = itFind->second;
  26. if( == nVal)
  27. {//删除该条数据
  28. mapDemo.erase(itFind);
  29. }
  30. }
  31. }
  32. }

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. 最新wap手机agent

    名称 agent 铃声格式 和弦数 数据量 删除 LGE-CU8080  LGE-CU8080/1.0 UP.Browser/4.1.26l UP.Link/5.1.2.9  pmd2.0  40   ...

  2. HBase GC日志

    HBase依靠ZooKeeper来感知集群成员及其存活性.假设一个server暂停了非常长时间,它将无法给ZooKeeper quorum发送心跳信息,其他server会觉得这台server已死亡.这 ...

  3. 我在开发第一个Swift App过程中学到的四件事

    本文转载至 http://www.itjhwd.com/wzkfyigeswiftsjs/ 译者注:本文作者Greg Heo,这是他为讲授iOS 8 App Extensions视频教程而实际使用Sw ...

  4. Net Core环境开发与调试

    NET Core 包括.NET Core Runtime 和 .NET Core SDK: .NET Core = 应用运行依赖的 .NET Core Runtime .NET Core SDK = ...

  5. c# winform 根据窗体自动调整控件

    一.概述 本文要实现的功能是:当窗体最大化时,控件的大小可以随窗体一起变化.开发环境,vs2010 c# winform,窗体名称采用默认的Form1. 2.把调整控件大小的方法放到一个类中:Form ...

  6. Time To First Byte (TTFB) 第一字节时间 页面加载时间

    Time to first byte - Wikipedia https://en.wikipedia.org/wiki/Time_to_first_byte Time to first byte ( ...

  7. Delphi里可将纯虚类实例化,还可调用非虚函数

    这是与Java/C++的巨大不同.目前还没仔细想这个特征与TClass之间的联系,先记住结论再说.以后再回来修改这个帖子. unit Unit1; interface uses Windows, Me ...

  8. JavaWeb浏览器传值乱码

    在Java服务接口调用中,可能会出现乱码,如下图所示: 这是由编码不一致而致的乱码,可转,如下: name = new String(name.getBytes("ISO-8859-1&qu ...

  9. CSU - 1803 —— 数学题

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1803 Description  给出正整数 n 和 m,统计满足以下条件的正整数对 ...

  10. hadoop集群部署后,遇到的问题记录

    1.  部署完,启动集群后,mapred-site.xml文件中配置没有生效 <property> <name>mapred.job.tracker</name> ...