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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 七、观察者模式——关注我,分享旅途最浪漫的瞬间! #和设计模式一起旅行#

    最浪漫的事就是在路上,身边有你陪伴!故事背景在路上,和设计模式MM,做过飞机,也骑过单车,从大中国到了东京,东京真的很热(知道那啥啥为什么叫东京热了吧)。这一路不断的分享我们走过的路和看过的风景,旅行就是如此的美好,看别人看的厌烦过的风景,走别人走了无数遍的路!只有自己走过了,才算是经历!(就如我的这一场设计模式的学习总计之路,不管有多少人分享过,我依然要自己在过一遍,其中的意…

    2022年2月27日
    164
  • 平民版均线量化交易模型

    平民版均线量化交易模型前言2021年转瞬即逝,回顾一下在蚂蚁上定投的基金,在金融危机风雨欲来的2022年,分享一个懒人版的理财策略,愿大家新年里能财源广进,元旦快乐。基金定投我的策略非常简单,每月无脑小额定投,…

    2022年6月26日
    42
  • VirtualBox命令行VBoxManage创建与管理虚拟机教程

    VirtualBox命令行VBoxManage创建与管理虚拟机教程VBoxManageisthecommand-lineinterfacetoVirtualBox.前言本文要操作的虚拟机信息如下:名称:UbuntuRDHome镜像名称:ubuntu-16.04.3-server-amd64.iso网络连接:桥接主机环境:$uname-a命令输出:LinuxUbuntuServer

    2022年6月8日
    29
  • python3中urlopen_Python3 urlopen()用法示例[通俗易懂]

    对python这个高级语言感兴趣的小伙伴,下面一起跟随编程之家jb51.cc的小编两巴掌来看看吧!一.简介urllib.request.urlopen()函数用于实现对目标url的访问。函数原型如下:urllib.request.urlopen(url,data=None,[timeout,]*,cafile=None,capath=None,cadefault=False,context=N…

    2022年4月7日
    93
  • mit6.824 lab4_mit6.830

    mit6.824 lab4_mit6.830一、 BETAISAA:0、1、2、3、0xcB:0x2000、0xEDEDEDED、0xFEDEDEDE、0x2004、11000000000111110010000000000000(0xc01f2000)C:0x87654321、1、0x87654320、0x14、01110111111000010000000000000010(0x77e10002)D:17、0、32、noinstructionsneedtobechangedE:4、110000000

    2022年9月30日
    2
  • es集群搭建_k8s和docker搭建es集群

    es集群搭建_k8s和docker搭建es集群单机的elasticsearch做数据存储,必然面临两个问题:海量数据存储问题、单点故障问题。ES集群搭建_使用docker-海量数据存储问题:将索引库从逻辑上拆分为N个分片(shard),存储到多个节点-单点故障问题:将分片数据在不同节点备份(replica)ES集群介绍为什么需要集群ES集群相关概念搭建ES集群集群职责划分集群脑裂问题…

    2022年10月12日
    4

发表回复

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

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