来源:(http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html) - C++ map的基本操作和使用_Live_新浪博客

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>  ( 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 ) );

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

[转载]STL map中的一些基本函数的更多相关文章

  1. stl map中的lower_bound和 upper_bound

    map中的lower_bound和upper_bound的意思其实很简单,就两句话: map::lower_bound(key):返回map中第一个大于或等于key的迭代器指针 map::upper_ ...

  2. (转载)Java Map中的Value值如何做到可以为任意类型的值

    转载地址:http://www.importnew.com/15556.html     如有侵权,请联系作者及时删除. 搬到我的博客来,有空细细品味,把玩. 本文由 ImportNew - shut ...

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

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

  4. STL map、set中key为结构体的用法

    下面是map定义的结构: // TEMPLATE CLASS map template<class _Kty, class _Ty, class _Pr = less<_Kty>, ...

  5. (转载)STL map与Boost unordered_map的比较

    原链接:传送门 今天看到 boost::unordered_map,它与 stl::map的区别就是,stl::map是按照operator<比较判断元素是否相同,以及比较元素的大小,然后选择合 ...

  6. Atcoder ABC155_C中有关c++ STL map的用法

    题目:https://atcoder.jp/contests/abc155/tasks/abc155_c 这道题的题意是给我们n个string,让我们统计每个string出现的次数,并输出次数最多的一 ...

  7. (转载)Java中如何遍历Map对象的4种方法

    在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...

  8. C++STL库中map容器常用应用

    #include<iostream> #include<cstdio> #include<map> //按键值大小构成二叉搜索树 using namespace s ...

  9. map中的erase成员函数用法

    转载于 http://www.cnblogs.com/graphics/archive/2010/07/05/1771110.html  http://hi.baidu.com/sdkinger/it ...

随机推荐

  1. Android 基础组件

    基础组件 所有的控件都可以在java代码中创建出来,并且大部分的属性都对应set和get方法,比如 View view = new View(Context context)  context是上下文 ...

  2. 在公网(internet)上建立website时不能用http访问

    我们在internet的IDC提供的虚拟机上建了新的web site, 域名也从阿里巴巴那里得到二级域名,也经过映射到了虚机的IP, 在本机都可以用域名以Http方式访问,但在internet上的其它 ...

  3. Android-Eclipse汉化

    首先下载好汉化包 解压后得到eclipse文件夹 然后在eclipse根目录下创建language文件夹 将eclipse文件夹放入 在eclipse根目录下创建links文件夹 然后在里面添加这个文 ...

  4. HTML基础总结<段落>

    HTML 段落 段落是通过 <p> 标签定义的. 实例 <p>This is a paragraph </p><p>This is another pa ...

  5. C++标准库<string>简单总结

    C++标准库<string>简单总结 在C++中,如果需要对字符串进行处理,那么它自带的标准库<string>无疑是最好的选择,它实现了很多常用的字符处理函数. 要想使用标准C ...

  6. 在vim中设置 '打印时间'的快捷键.

    在 ~/.vimrc (没有该文件可以手动创建)中输入 map <F4> <Esc>:r !date<CR> 实现在 '一般模式'状态点击 F4时,自动在vim中打 ...

  7. linux下查看已经安装的jdk 并卸载jdk

    一.查看Jdk的安装路径: whereis javawhich java (java执行路径)echo $JAVA_HOME echo $PATH 备注:如果是windows中,可以使用: set j ...

  8. linux 第三天11讲

    1.linux下的shell 查看目前使用的是哪种shell env [该命令可以显示当前操作系统的环境变量] env | more chsh -s 输入新的shell 如:/bin/csh shel ...

  9. CSS中的盒子模型详解

    很多人对盒子模型搞晕头了,下面通过一个简单的代码来分析盒子模型的结构! 为了方便方便观看!在第一个div中画了一个表格,并将其尺寸设置成与div内容大小一样!且设置body的margin和paddin ...

  10. oracle单行函数之数字函数

    round--四舍五入函数 trunc--截断函数 mod--求余函数 round和trunc除了可以操作数字外还可以操作日期. Demo SQL),),),),) from dual ; ) ) ) ...