STL之Map的运用

STL之Map的运用

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
  请按随意键继续. . .

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
    char str[50];
    int count=0;
    map <string,int> counter;
map <string,int> ::iterator it;
    while(gets(str)!=NULL)
    {
        counter[str]++;
        count++;
    }
    for(it=counter.begin();it!=counter.end();it++)
    {
cout<<it->first<<” “<<it->second<<endl;
    }
    return 0;
}

7,   map的基本操作函数:
      C++ Maps是一种关联式容器,包括“keyword/值”对
      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的函数

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/119065.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 为什么投屏找不到设备(投屏电视显示无法访问服务器)

    现在液晶电视价格越来越便宜,很少的钱就可以买一台60寸以上的电视,那么使用电脑的朋友一定想要把画面投屏到电视上,用于玩游戏、看电影吧!Win10就有非常好用的投屏功能,很多朋友可能不知道如何操作,这里小编和大家分享下具体步骤:Win10投屏电视步骤如下:(以小米电视为例)1、首先将电脑连接无线WIFI。2、将电视也连接在同一个无线WIFI网络下。3、进入电视应用中,选择“无线显示”功能。(不同的型…

    2022年4月15日
    754
  • 给定一个n个正整数组成的数组_算法基础课acwing下载

    给定一个n个正整数组成的数组_算法基础课acwing下载给定一个长度为 N 的数列 A,以及 M 条指令,每条指令可能是以下两种之一:C l r d,表示把 A[l],A[l+1],…,A[r] 都加上 d。Q l r,表示询问数列中第 l∼r 个数的和。对于每个询问,输出一个整数表示答案。输入格式第一行两个整数 N,M。第二行 N 个整数 A[i]。接下来 M 行表示 M 条指令,每条指令的格式如题目描述所示。输出格式对于每个询问,输出一个整数表示答案。每个答案占一行。数据范围1≤N,M≤105,|d|≤10000,|A[i]|≤1

    2022年8月9日
    6
  • 时钟周期,机器周期,指令周期的区别是什么_紫外分光吸光度大于1

    时钟周期,机器周期,指令周期的区别是什么_紫外分光吸光度大于1时钟周期     时钟周期也称为振荡周期,定义为时钟脉冲的倒数(时钟周期就是单片机外接晶振的倒数,例如12M的晶振,它的时钟周期就是1/12us),是计算机中的最基本的、最小的时间单位。    在一个时钟周期内,CPU仅完成一个最基本的动作。时钟脉冲是计算机的基本工作脉冲,控制着计算机的工作节奏。时钟频率越高,工作速度就越快。     8051单片机把一个时钟

    2022年10月13日
    3
  • 就业更看重本科学历还是研究生学历(女孩没学历做什么工作好)

    前言我是一个普通本科出身的Android程序员,我的学校也不过就是一个普通二本。嗯,我的学弟学妹们也是一样的,都是普通二本。但是和我不同的是,现在的社会越来越浮躁了,浮躁的让人沉不下心认真做事,让人忍不住去想各种有的没的。比如我的这些学弟学妹们。我已经不止一次收到来自他们的私信了,他们问的内容,无一不是表达对自己学历的自卑和对即将离开学校的自己的不自信,还有对面试被拒的伤心。千篇一律的问题,基本内容如下:面试挂了,大厂面试到底更看重学历还是技术?我这样的学历在求职中有什么需要注意点的点吗?

    2022年4月13日
    109
  • Windows 10版本business_editions和consumer_editions的区别?「建议收藏」

    Windows 10版本business_editions和consumer_editions的区别?「建议收藏」Windows10版本business_editions和consumer_editions的区别?【答1】二者都内置专业版,不同之处在于:consumer_editions版本包含:Home(家庭版);Education(教育版);Professional(专业版);business_editions版本包含:Education(教育版);Enterprise(企业版);Pro…

    2025年7月9日
    3
  • 如何修改redis的端口号_redis配置文件详解

    如何修改redis的端口号_redis配置文件详解redis修改默认端口的方法是:首先要先下载文件并解压编译及安装,安装好后全局启动并且设置密码,然后再修改端口号,最后指定运行配置即可【推荐课程:redis教程】(1)通过下面的链接进行下载,然后再用以下命令进行,解压,编译,安装下载地址:http://download.redis.io/redis-stable.tar.gztarxzfredis-4.0.9.tar.gzcdredis-4…

    2022年9月18日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号