STL-map/multimap容器

简介: STL-map/multimap容器

  • 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;
}
目录
相关文章
|
17天前
|
存储 算法 NoSQL
C++一分钟之-map与set容器详解
【6月更文挑战第21天】C++ STL的`map`和`set`是基于红黑树的关联容器,提供有序存储和高效查找。`map`存储键值对,键唯一,值可重复;`set`仅存储唯一键。两者操作时间复杂度为O(log n)。常见问题包括键的唯一性和迭代器稳定性。自定义比较函数可用于定制排序规则,内存管理需注意适时释放。理解和善用这些工具能提升代码效率。
14 3
|
1月前
|
存储 人工智能 C++
map容器在C++中的具体用法以及相关注意点
map容器在C++中的具体用法以及相关注意点
20 1
|
12天前
|
C++ 容器
C++ STL标准库 《map容器详解》
C++ STL标准库 《map容器详解》
12 0
|
12天前
|
存储 C++ 容器
C++ STL标准库 《map容器详解》
C++ STL标准库 《map容器详解》
14 0
|
12天前
|
C++ 容器
C++之map/multimap容器
C++之map/multimap容器
10 0
|
13天前
|
编译器 C++ 容器
通过红黑树封装 map 和 set 容器
通过红黑树封装 map 和 set 容器
|
21天前
|
存储 安全 C++
Map容器详解
Map容器详解
|
2月前
|
C++ 索引 容器
黑马c++ STL部分 笔记(9) map/multimap容器
黑马c++ STL部分 笔记(9) map/multimap容器
|
19天前
|
前端开发 安全 数据库
Web架构&前后端分离站&Docker容器站&集成软件站&建站分配
Web架构&前后端分离站&Docker容器站&集成软件站&建站分配