前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c++STL容器之map容器

c++STL容器之map容器

作者头像
西西嘛呦
发布2020-08-26 21:24:24
4310
发布2020-08-26 21:24:24
举报

1.map中所有的元素都是pair;

2.pair元素中第一个元素为key,第二个元素为value;

3.所有元素都会根据键值自动排序;

4.map中不允许有重复的键,multimap中允许有重复的键;

优点:可以根据key快速的找到value;

一、构造函数

代码语言:javascript
复制
map<T1,T2> mp;
map(const map &mp);

二、赋值

代码语言:javascript
复制
map& operator=(const map &mp);

三、map大小和交换

代码语言:javascript
复制
size();
empty();
swap(st);

四、插入和删除

代码语言:javascript
复制
insert(ele);
clear();
erase(pos);
erase(beg,end);
erase(key);
代码语言:javascript
复制
#include<iostream>
using namespace std;
#include <map>

//map容器 插入和删除

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;
}

void test01()
{
    map<int, int>m;

    //插入
    //第一种
    m.insert(pair<int, int>(1, 10));

    //第二种
    m.insert(make_pair(2, 20));

    //第三种
    m.insert(map<int, int>::value_type(3, 30));

    //第四种
    m[4] = 40;

    //[]不建议插入,用途 可以利用key访问到value
    //cout << m[4] << endl;
    printMap(m);


    //删除
    m.erase(m.begin());
    printMap(m);


    m.erase(3); //按照key删除
    printMap(m);

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

int main() {

    test01();

    system("pause");

    return 0;
}

五、查找和统计

代码语言:javascript
复制
void test01()
{
    //查找
    map<int, int>m;
    m.insert(pair<int, int>(1, 10));
    m.insert(pair<int, int>(2, 20));
    m.insert(pair<int, int>(3, 30));
    m.insert(pair<int, int>(3, 40));

    map<int,int>::iterator pos = m.find(3);

    if (pos != m.end())
    {
        cout << "查到了元素 key = " << (*pos).first << " value = " << pos->second << endl;
    }
    else
    {
        cout << "未找到元素" << endl;
    }

    //统计
    //map不允许插入重复key 元素 ,count统计而言 结果要么是0 要么是1
    //multimap的count统计可能大于1
    int num = m.count(3);
    cout << "num = " << num << endl;
}

六、map排序(按升序排序)

代码语言:javascript
复制
#include<iostream>
using namespace std;
#include <map>

class MyCompare
{
public:
    bool operator()(int v1,int v2) const
    {
        //降序
        return v1 > v2;
    }
};

//map容器 排序
void test01()
{
    map<int, int , MyCompare>m;

    m.insert(make_pair(1, 10));
    m.insert(make_pair(2, 20));
    m.insert(make_pair(5, 50));
    m.insert(make_pair(3, 30));
    m.insert(make_pair(4, 40));


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

int main() {

    test01();

    system("pause");

    return 0;
}

vs2019在重载operator()时需要用const修饰。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-12-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档