STL-map/multimap容器

104 阅读2分钟

简介:

  • map中所有元素都是pair
  • pair第一个元素为key键值,起到索引作用,第二个元素为value实质
  • 所有元素都会按照key键值自动排序

本质:

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

优点:

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

map和multimap区别:

  • map不允许容器中有重复的key值元素
  • multimap允许容器中有重复的key值元素

map构造和赋值

构造:

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

赋值:

  • map& operator=(const map& mp);//重载=运算符

map容器所有元素都成对出现,插入数据需要使用对组

#include<iostream>
#include<string>
#include<map>
using namespace std;
void PrintMap(const map<int, int>& mp)
{
    for (map<int, int>::const_iterator it = mp.begin(); it != mp.end(); it++)
    {
        //可以用解引用的方法,也可以用间接访问操作符->
        cout << (*it).first << " " << it->second << endl;
    }
    cout << endl;
}
int main()
{
    map<int, int>mp;
    mp.insert(pair<int, int>(1, 2));
    mp.insert(pair<int, int>(5, 4));
    mp.insert(pair<int, int>(3, 1));
    mp.insert(pair<int, int>(2, 4));
    mp.insert(pair<int, int>(4, 6));
    PrintMap(mp);
    map<int, int>mp1;
    mp1 = mp;
    PrintMap(mp1);
    return 0;
}

map容器大小和交换

  • .size();//返回容器中元素数目
  • .empty();//判断容器是否为空
  • .swap(mp);//交换两个集合容器
#include<iostream>
#include<string>
#include<map>
using namespace std;
void PrintMap(const map<int, int>& mp)
{
    for (map<int, int>::const_iterator it = mp.begin(); it != mp.end(); it++)
    {
        //可以用解引用的方法,也可以用间接访问操作符->
        cout << (*it).first << " " << it->second << endl;
    }
    cout << mp.size() << mp.empty() << endl;
}
int main()
{
    map<int, int>mp;
    map<int, int>mp1;
    mp.insert(pair<int, int>(1, 2));
    mp.insert(pair<int, int>(5, 4));
    mp.insert(pair<int, int>(3, 1));
    mp1.insert(pair<int, int>(2, 4));
    mp1.insert(pair<int, int>(4, 6));
    mp1.insert(pair<int, int>(6, 6));
    PrintMap(mp);
    PrintMap(mp1);
    //交换
    mp.swap(mp1);
    //验证交换后的结果
    PrintMap(mp);
    PrintMap(mp1);
    return 0;
}

map插入和删除

  • .insert(elem);//容器中插入元素
    • pair方式
    • make_pair方式
    • value_type方式
    • []下标方式
  • .clear();//清除所有元素
  • .erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器
  • .erase(beg,end);//删除区间[beg,end)内的元素,返回下一个元素的迭代器
  • .erase(key);//删除容器中值为key的元素
#include<iostream>
#include<string>
#include<map>
using namespace std;
void PrintMap(const map<int, int>& mp)
{
    for (map<int, int>::const_iterator it = mp.begin(); it != mp.end(); it++)
    {
        //可以用解引用的方法,也可以用间接访问操作符->
        cout << (*it).first << " " << it->second << endl;
    }
    cout << endl;
}
int main()
{
    map<int, int>mp;
    //第一种插入
    mp.insert(pair<int, int>(2, 1));
    //第二种插入,不需要写模板参数
    mp.insert(make_pair(1, 3));
    //第三种,value_type值类型
    mp.insert(map<int, int>::value_type(3, 6));
    //第四种,不建议用于插入
    mp[4] = 5;
    //下标方式,如果没有找到,会创建一个实值为0的元素
    //作用:可以利用key值访问到value
    cout << mp[5] << endl;
    PrintMap(mp);
    //删除
    mp.erase(mp.begin());
    mp.erase(5);//按照key删除
    PrintMap(mp);
    return 0;
}

map查找和统计

  • .find(key);//查找key是否存在,返回该键的元素的迭代器,若不存在,返回.end();
  • .const(key);//统计key的元素个数,对于map,无非是0或1
map<int, int>mp;
mp.insert(pair<int, int>(1, 2));
mp.insert(make_pair(2, 2));
mp.insert(map<int, int>::value_type(3, 1));
if (mp.find(3) != mp.end())
{
    cout << "找到了" << mp[3] << endl;
}

map自定义排序规则

利用仿函数,可以改变排序规则
对于自定义数据类型,map必须指定排序规则,同set容器

#include<iostream>
#include<string>
#include<map>
using namespace std;
class myCompare
{
public:
    bool operator()(int v1, int v2)const
    {
        return v1 > v2;
    }
};
int main()
{
    map<int, int, myCompare>mp;
    mp.insert(pair<int, int>(1, 2));
    mp.insert(make_pair(2, 2));
    mp.insert(map<int, int>::value_type(3, 1));
    for (map<int, int, myCompare>::iterator it = mp.begin(); it != mp.end(); it++)
    {
        cout << it->first << " " << it->second << endl;
    }
    return 0;
}