c++学习之map容器

map的基本概念

简介

map中所有的元素都是pair
pair中第一个元素为key值(键值),起到索引作用,第二个元素为value(实值)
所有元素都会根据元素的键值自动排序

本质

map/multimap属于关联式容器,底层结构是用二叉树实现的

优点

可以根据key值快速找到value值

map和multimap区别

map容器中不允许有重复的key值(value可以重复)。
multimap允许容器中有重复的key值

map构造和赋值

函数原型

map< T1,T2>m; //map默认构造函数
map ,m1( const map &m); //拷贝构造函数
map& operator = (const map &m); //重载运算符赋值

代码示例

#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;

void printmap(const map<int,string>& m)
{
      cout<<"*************************"<<endl;
      for(map<int,string>::const_iterator it = m.begin();it != m.end(); it++)
      {
          cout<<"key = "<< it->first <<" "<<"value ="<<(*it).second << endl;
      }
}
void test()
{
  //1.默认构造
  map<int,string>m; 
  m.insert(pair<int,string>(1,"lcl"));  //插入元素
  m.insert(pair<int,string>(3,"xqq"));
  m.insert(pair<int,string>(4,"hqq"));
  m.insert(pair<int,string>(2,"wxr"));
  printmap(m);
   //2.拷贝构造
   map<int,string>m1(m);
   printmap(m1);
   //3.‘=’
   map<int,string>m2;
   m2 = m1;
   printmap(m2);
}
int main()
{ 
    test();
    return 0;
}

输出结果
在这里插入图片描述

map大小和交换

函数原型

size()
empty()
swap(m)

代码示例

#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;

void printmap(const map<int,string>& m)
{
      cout<<"*************************"<<endl;
      for(map<int,string>::const_iterator it = m.begin();it != m.end(); it++)
      {
          cout<<"key = "<< it->first <<" "<<"value ="<<(*it).second << endl;
      }
}

void test()
{
  //1.默认构造
  map<int,string>m; 
  m.insert(pair<int,string>(1,"lcl"));  //插入元素
  m.insert(pair<int,string>(3,"xqq"));
  m.insert(pair<int,string>(4,"hqq"));
  m.insert(pair<int,string>(2,"wxr"));
  //size 和 empty 
   if(!m.empty())
   {
      cout<< "m的size ="<<m.size() << endl;
   }
  //swap
  map<int,string>m3; 
  m3.insert(pair<int,string>(1,"lll"));  //插入元素
  m3.insert(pair<int,string>(3,"xxx"));
  m3.insert(pair<int,string>(4,"qqq"));
  m3.insert(pair<int,string>(2,"www"));
  
  cout<<"before swap" <<endl;
  printmap(m);
  printmap(m3);
  cout<<"after swap"<<endl;
  m3.swap(m);
  printmap(m);
  printmap(m3);

}

int main()
{ 
    test();
    return 0;
}

输出结果
在这里插入图片描述

map的插入和删除

函数原型

insert(elem);
clear()
erase(pos) //删除pos迭代器所指的元素,返回下一个元素的迭代器
erase(beg,end) //区间删除,返回下一个元素的迭代器
erase(key) //删除容器中键值为key的元素

代码示例(插入的四种方法)

#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;

void printmap(const map<int,string>& m)
{
      cout<<"*************************"<<endl;
      for(map<int,string>::const_iterator it = m.begin();it != m.end(); it++)
      {
          cout<<"key = "<< it->first <<" "<<"value ="<<(*it).second << endl;
      }
}

void test()
{
  //1.默认构造
  map<int,string>m; 
  //第一种
  m.insert(pair<int,string>(1,"lcl"));  
  //第二种
  m.insert(make_pair(2,"xqq"));
  //第三中
  m.insert(map<int,string>::value_type(3,"wxr"));
  //第四种
  m[4] = "hqq"; //[]重载
  printmap(m);

  //[]不建议插入,用途,可以利用key访问到value
  cout<< m[4] << endl;
  cout<< m[2] << endl;
}
int main()
{ 
    test();
    return 0;
}

输出结果
在这里插入图片描述
代码示例(删除操作)

#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;

void printmap(const map<int,string>& m)
{
      cout<<"*************************"<<endl;
      for(map<int,string>::const_iterator it = m.begin();it != m.end(); it++)
      {
          cout<<"key = "<< it->first <<" "<<"value ="<<(*it).second << endl;
      }
}

void test()
{
  //1.默认构造
  map<int,string>m; 
  //第一种
  m.insert(pair<int,string>(1,"lcl"));  
  //第二种
  m.insert(make_pair(2,"xqq"));
  //第三中
  m.insert(map<int,string>::value_type(3,"wxr"));
  //第四种
  m[4] = "hqq"; //[]重载
  map<int,string>m1(m);
  printmap(m);

  map<int,string>::iterator it = m.erase(m.begin());
  cout<< it->first <<" "<<it->second<<endl;
  m.erase(--m.end());
  printmap(m);

  m1.erase(3);   //重载
  printmap(m1);

  m1.erase(2);   //重载
  printmap(m1);

  //清空
  m.clear();
  m1.erase(m1.begin(),m1.end());
  printmap(m1);
  printmap(m);
}

int main()
{ 
    test();
    return 0;
}

输出结果
在这里插入图片描述

map容器-查找和统计

函数原型

find(key) //查找key是否存在,存在返回该键值的迭代器,不存在则返回end迭代器
count(key)//统计key的元素个数

#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;

void printmap(const map<int,string>& m)
{
      cout<<"*************************"<<endl;
      for(map<int,string>::const_iterator it = m.begin();it != m.end(); it++)
      {
          cout<<"key = "<< it->first <<" "<<"value ="<<(*it).second << endl;
      }
}

void test()
{
  //1.默认构造
  map<int,string>m; 
  //第一种
  m.insert(pair<int,string>(1,"lcl"));  
  //第二种
  m.insert(make_pair(2,"xqq"));
  //第三中
  m.insert(map<int,string>::value_type(3,"wxr"));
  //第四种
  m[4] = "hqq"; //[]重载
  map<int,string>m1(m);
  printmap(m);

  map<int,string>::iterator it = m.find(4);
  cout<< it->first <<" "<<it->second<<endl;

  cout<< m.count(2) << endl;
  cout<< m.count(5) << endl;
}
int main()
{ 
    test();
    return 0;
}

输出结果
在这里插入图片描述

map容器的指定排序

默认是从小到大,现在指定从大到小

#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;

class myCompare
{
  public:
    bool operator()(int v1,int v2)
    {
        return v1 > v2;
    }
};

void printmap(const map<int,int,myCompare>& m)
{
      cout<<"*************************"<<endl;
      for(map<int,int,myCompare>::const_iterator it = m.begin();it != m.end(); it++)
      {
          cout<<"key = "<< it->first <<" "<<"value ="<<(*it).second << endl;
      }
}

void test()
{
    map<int,int,myCompare>m;
    m.insert(make_pair(1,10));
    m.insert(make_pair(2,20));
    m.insert(make_pair(3,30));
    m.insert(make_pair(4,40));
    m.insert(make_pair(5,50));

    printmap(m);
}

int main()
{ 
    test();
    return 0;
}

输出结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

<( ̄︶ ̄)Okay.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值