c++ map容器/multimap容器

820 阅读3分钟

这是我参与更文挑战的第9天,活动详情查看: 更文挑战

1. map基本概念

(1)简介:

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

(2)本质:

map/multimap属于关联式容器,底层采用二叉树实现

(3)优点:

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

(4)map与multimap的区别:

  • map不允许容器中有重复的key值,value无所谓
  • multimap允许容器中有重复的key值

2. map构造函数

  • map<T1, T2> mp; //map默认构造函数
  • map(const map &mp); //拷贝构造函数

3. map赋值函数

  • map& operator = (const map &mp) ; //重载等号操作符
#include<iostream>
#include<map>
using namespace std;

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

//map容器构造和赋值
void test()
{
    //创建map容器
   map<int,int> m;

   m.insert(pair<int, int>(1,10));//key值为1,实值为10
   m.insert(pair<int, int>(3,10));//key值为2,实值为10
   m.insert(pair<int, int>(2,10));//key值为3,实值为10
   m.insert(pair<int, int>(4,10));//key值为4,实值为10
   
   //拷贝构造函数
   map<int, int>m2(m);
   
   printMap(m2);
   printMap(m);

   //赋值
   map<int, int>m3;
   m3 = m2;
   printMap(m3);
}

int main(){
    test();
}

输出结果:

key = 1 value = 10
key = 2 value = 10
key = 3 value = 10
key = 4 value = 10

key = 1 value = 10
key = 2 value = 10
key = 3 value = 10
key = 4 value = 10

key = 1 value = 10
key = 2 value = 10
key = 3 value = 10
key = 4 value = 10

4. map大小与交换

(1)函数原型

  • size() ; //返回容器中元素数目
  • empty() ; //判断容器是否为空
  • swap() ; //交换两个集合容器

(2) 示例

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

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

//map容器构造和赋值
void test()
{
    //创建map容器
   map<int,int> m;

   m.insert(pair<int, int>(1,10));//key值为1,实值为10
   m.insert(pair<int, int>(3,10));//key值为2,实值为10
   m.insert(pair<int, int>(2,10));//key值为3,实值为10
   m.insert(pair<int, int>(4,10));//key值为4,实值为10
   
   if(m.empty())
   {
       cout<<"空"<<endl;
   }
   else
   {
       cout<<"不空"<<endl;
       cout<<m.size()<<endl;
   }

   map<int,int> m2;

   m2.insert(pair<int, int>(1,30));
   m2.insert(pair<int, int>(3,160));
   m2.insert(pair<int, int>(2,102));
   m2.insert(pair<int, int>(4,105));
   
   cout<<"交换前:"<<endl;
   printMap(m);
   printMap(m2);

   //swap()交换容器中值
   m.swap(m2);

   cout<<"交换后"<<endl;
   printMap(m);
   printMap(m2);
}

int main(){
    test();
}

输出结果:

不空
4
key = 1 value = 30
key = 2 value = 102
key = 3 value = 160
key = 4 value = 105

key = 1 value = 10
key = 2 value = 10
key = 3 value = 10
key = 4 value = 10

5. map中插入与删除操作

(1)函数原型

  • insert(elem); //在容器中插入元素
  • clear(); //清除所有元素
  • erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器
  • erase(beg, end); //删除区间[beg, end]的所有元素,返回下一个元素的迭代器
  • erase(key); //删除容器中值为key的元素

(2)map容器插入的几种方式

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

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

//map容器插入与删除
void test()
{
    //创建map容器
   map<int,int> m;

   //插入第一种
   m.insert(pair<int, int>(1,10));//key值为1,实值为10
   m.insert(pair<int, int>(3,10));//key值为2,实值为10
   m.insert(pair<int, int>(2,10));//key值为3,实值为10
   m.insert(pair<int, int>(4,10));//key值为4,实值为10
   
   //第二种,采用模版make_pair
   m.insert(make_pair(5,100));

   //第三种,麻烦
   m.insert(map<int, int>::value_type(6,99));

   //第四种,采用重载的[],不建议用[],因为[],会创造出不存在的数,但可以通过[],key的值访问value
   m[7] = 128;

   cout << m[8]<<endl;//没有插入m[8],但却能输出0

   printMap(m);

}

int main(){
    test();
}

输出结果:

0
key = 1 value = 10
key = 2 value = 10
key = 3 value = 10
key = 4 value = 10
key = 5 value = 100
key = 6 value = 99
key = 7 value = 128
key = 8 value = 0

6. map查找和统计

(1)函数原型

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

(2) 示例

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

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

//map容器查找和统计
void test()
{
    //创建map容器
   map<int,int> m;

   m.insert(pair<int, int>(1,10));//key值为1,实值为10
   m.insert(pair<int, int>(3,10));//key值为2,实值为10
   m.insert(pair<int, int>(2,10));//key值为3,实值为10
   m.insert(pair<int, int>(4,10));//key值为4,实值为10
   
   map<int, int>::iterator pos = m.find(5); 
   if(pos != m.end())
   {
       cout<<"查到了元素 key = "<<(*pos).first << "value = " <<pos->second <<endl;
   }
   else
   {
       cout<<"没找到"<<endl;
   }

    //map中找出来只可能为1,multimap可能不为1
   int num = m.count(3);
   cout<<num<<endl;

   printMap(m);

  
}

int main(){
    test();
}

输出结果:

没找到
1
key = 1 value = 10
key = 2 value = 10
key = 3 value = 10
key = 4 value = 10

7. map容器排序

map容器默认排序是按照key值进行从小到大排序,掌握如何改变排序规则。

主要利用彷函数,可以改变排序规则。