C++学习第二十四天(map容器)

  1. map容器概念
    简介:map中所有元素都是pair;
    pair中第一个元素为key(键值),起到索引作用,第二个为value(实值);所有元素都会根据元素的键值自动排序。

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

优点:可以根据key值快速找到value值。

map和multimap的区别:map不允许容器中有重复的key值,multimap允许。

  1. map的构造和赋值
#include <iostream>
#include <map>

using namespace std;

//打印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;;
    }
}

void test01()
{
    //创建map容器
    map<int, int> m1;

    //会按照key值排序
    m1.insert(pair<int, int>(1,10));
    m1.insert(pair<int, int>(3,40));
    m1.insert(pair<int, int>(4,20));
    m1.insert(pair<int, int>(2,30));
    printMap(m1);

    //拷贝构造
    map<int, int> m2(m1);
    printMap(m2);

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

int main(void)
{
    test01();

    return 0;
}

map容器的大小及交换:

#include <iostream>
#include <map>

using namespace std;

//打印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;;
    }
}

void test01()
{
    map<int, int> m1;
    map<int, int> m2;

    m1.insert(pair<int, int>(1,10));
    m1.insert(pair<int, int>(2,20));
    m1.insert(pair<int, int>(3,30));
    m2.insert(pair<int, int>(4,10));
    m2.insert(pair<int, int>(5,20));
    m2.insert(pair<int, int>(6,30));
    
    cout << "交换前==============" << endl;

    printMap(m1);
    printMap(m2);

    if(m1.empty())
    {
        cout << "m1为空" << endl;
    }
    else
    {
        cout << "m1不为空" << endl;
        cout << "m1的大小为:" << m1.size() << endl;
    }

    //交换
    m1.swap(m2);
    cout << "交换后=============" << endl;

    printMap(m1);
    printMap(m2);
}

int main(void)
{
    test01();

    return 0;
}

map容器插入和删除

#include <iostream>
#include <map>

using namespace std;

//打印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;;
    }
}

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

    //插入(第一种insert)
    m1.insert(pair<int, int>(1,10));
    //第二种insert
    m1.insert(make_pair(2, 20));
    //第三种insert
    m1.insert(map<int, int>::value_type(3,30));
    //第四种insert
    m1[4] = 40;//不建议使用原因如下
    //因为会自动创建一个键值队,默认值为0
    //cout << m1[5] << endl;
    //[]不建议插入使用,一般用于利用key值访问value
    //cout << m1[4] << endl;
    printMap(m1);

    //删除
    m1.erase(m1.begin());
    //只能按照key值删除
    m1.erase(3);
    //区间删除
    m1.erase(m1.begin(), m1.end());
    printMap(m1);

    //清空
    m1.clear();
    printMap(m1);

}

int main(void)
{
    test01();

    return 0;
}

map容器查找和统计

#include <iostream>
#include <map>

using namespace std;

//打印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;;
    }
}

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

    m1.insert(pair<int, int>(1, 10));
    m1.insert(pair<int, int>(2, 10));
    m1.insert(pair<int, int>(3, 20));
    m1.insert(pair<int, int>(3, 30));
    m1.insert(pair<int, int>(6, 10));

    //查找键值,并返回一个迭代器
    map<int, int>::iterator pos = m1.find(6);

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

    //统计键值为3的个数(map容器不允许插入同样键值元素)
    //结果只能为0或者1
    int num = m1.count(3);
    cout << "num = " << num << endl;
}

int main(void)
{
    test01();

    return 0;
}

map容器的排序(默认是按照键值从小到大排序):

#include <iostream>
#include <map>

using namespace std;

class myCompare
{
public:
    //自己写的仿函数
    bool operator()(int v1, int v2)
    {
        //键值降序
        return v2 < v1;
    }
};

void test01()
{
    map<int, int, myCompare> m1;//利用模板的构造和仿函数实现键值降序排序

    m1.insert(pair<int, int>(1, 10));
    m1.insert(pair<int, int>(2, 20));
    m1.insert(pair<int, int>(3, 30));
    m1.insert(make_pair(4, 40));
    m1.insert(make_pair(5, 50));

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

int main(void)
{
    test01();

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值