33,map/multimap容器

33.1map基本概念

简介:

map中所有元素都是pair(对组)

pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)

所有元素都会根据元素的键值自动排序

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

优点:

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

map和multimap区别:

map不允许容器中有重复的key值元素(value值可以有重复)

multimap润许容器中有重复的key值元素

33.2map构造和赋值

功能描述:对map容器进行构造和赋值操作

函数原型:

构造:

map<T1,T2>mp;                              //map默认构造函数

map(const map &mp);                     //拷贝构造函数

赋值:

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

void test01()
{
    map<int, int> mp1;
    mp1.insert(pair<int, int>(1, 10));
    mp1.insert(pair<int, int>(4, 40));
    mp1.insert(pair<int, int>(2, 20));
    mp1.insert(pair<int, int>(3, 30));
    printMap(mp1);
    cout << "------------------------------" << endl;
    map<int, int> mp2(mp1);
    printMap(mp2);
    cout << "------------------------------" << endl;
    map<int, int> mp3;
    mp3 = mp2;
    printMap(mp3);
}

int main()
{
    test01();
    system("pause");
    return 0;
}

运行结果:

key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
key = 4 value = 40
------------------------------
key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
key = 4 value = 40

------------------------------
key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
key = 4 value = 40

 总结:map中所有元素都是成对出现,插入元素时候要使用对组

33.3map大小和交换

功能描述:

统计map容器大小以及交换map容器

函数原型:

size();       //返回容器元素的数目

empty();    //判断容器是否为空

swap();      //交换两个容器集合

#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;
    }
}
/*
void printMap2(map<int, string>& m)
{
    for (map<int, string>::iterator it = m.begin(); it != m.end(); it++)
    {
        cout << "key=" << it->first << " value=" << it->second << endl;
    }
}
*/
void test01()
{
    map<int, int> mp1;
    mp1.insert(pair<int, int>(1, 10));
    mp1.insert(pair<int, int>(4, 40));
    mp1.insert(pair<int, int>(2, 20));
    mp1.insert(pair<int, int>(3, 30));
    
    if (mp1.empty())
    {
        cout << "mp1为空" << endl;
    }
    else
    {
        cout << "mp1不为空" << endl;
        cout << "mp1中元素数目:" << mp1.size() << endl;
    }
   /* 
    map<int, string>mp2;
    mp2.insert(pair<int, string>(3, "张三"));
    mp2.insert(pair<int, string>(6, "赵六"));
    mp2.insert(pair<int, string>(4, "李四"));
    mp2.insert(pair<int, string>(5, "王五"));
    printMap2(mp2);
    mp1.swap(mp2);
    */
    //以上注释代码会报错,因为类型不同
    map<int, int>mp2;
    mp2.insert(pair<int, int>(100, 1000));
    mp2.insert(pair<int, int>(400, 4000));
    mp2.insert(pair<int, int>(200, 2000));

    cout << "mp1元素:" << endl;
    printMap(mp1);

    cout << "mp2元素:" << endl;
    printMap(mp2);

    mp1.swap(mp2);

    cout << "mp1元素:" << endl;
    printMap(mp1);

    cout << "mp2元素:" << endl;
    printMap(mp2);
}
int main()
{
    test01();
    system("pause");
    return 0;
}

输出结果:

mp1不为空
mp1中元素数目:4
mp1元素:
key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
key = 4 value = 40
mp2元素:
key = 100 value = 1000
key = 200 value = 2000
key = 400 value = 4000
mp1元素:
key = 100 value = 1000
key = 200 value = 2000
key = 400 value = 4000
mp2元素:
key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
key = 4 value = 40

总结:

判断大小--size

判断是否为空--empty

交换容器--swap

33.4map插入和删除

功能描述:

map容器进行插入数据和删除数据

函数原型:

insert(elem);      //在容器中插入元素

clear();               //清除所有元素

erase(pos);        //删除pos迭代器所指的元素,返回下一个元素的迭代器

erase(beg,end); //删除区间[beg,end)的所有元素,返回下一个元素的迭代器

erase(key);        //删除容器中值为key的元素

#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;
}
void test01()
{
    map<int, int>mp1;
    //第一种插入方式
    mp1.insert(make_pair(6, 60));
    //第二种插入方式
    mp1.insert(pair<int, int>(1, 10));
    mp1.insert(pair<int, int>(3, 30));
    mp1.insert(pair<int, int>(2, 20));
    mp1.insert(pair<int, int>(4, 40));
    mp1.insert(pair<int, int>(5, 50));
    //第三种插入方式(不建议用,太长)
    mp1.insert(map<int, int>::value_type(7, 70));
    //第四种插入方式(重载了中括号,不建议用)
    mp1[8] = 80;
    cout << mp1[9] << endl;//会输出0
    //会使输出结果多出一个(9,0),所以不建议用,因为如果插错了会自动创建
    //[]不建议插入,用途 可以利用key访问到value
    printMap(mp1);

    mp1.erase(3);
    printMap(mp1);

    mp1.erase(mp1.begin());
    printMap(mp1);

    map<int, int>::iterator it = mp1.begin();
    mp1.erase(++it, mp1.end());//要注意先++it,it++则结果不同
    printMap(mp1);

    mp1.clear();
    if (mp1.empty())
    {
        cout << "s1为空" << endl;
    }
    else
    {
        cout << "s1不为空" << endl;
    }
}
int main()
{
    test01();
    system("pause");
    return 0;
}

0
key=1 value=10
key=2 value=20
key=3 value=30
key=4 value=40
key=5 value=50
key=6 value=60
key=7 value=70
key=8 value=80
key=9 value=0

key=1 value=10
key=2 value=20
key=4 value=40
key=5 value=50
key=6 value=60
key=7 value=70
key=8 value=80
key=9 value=0

key=2 value=20
key=4 value=40
key=5 value=50
key=6 value=60
key=7 value=70
key=8 value=80
key=9 value=0

key=2 value=20

s1为空

总结:

map插入方式很多,记住一种即可

插入--insert

删除--erase

清空--clear

33.5map查找和统计

功能描述:

对map容器进行查找数据以及统计数据

函数原型:

find(key);      //查找key是否存在,若存在,返回该键的元素迭代器;若不存在返回set.end();

count(key);   //统计key的元素个数

#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;
}
void test01()
{
    map<int, int>mp1;
    mp1.insert(pair<int, int>(1, 10));
    mp1.insert(pair<int, int>(3, 30));
    mp1.insert(pair<int, int>(2, 20));
    mp1.insert(pair<int, int>(4, 40));
    mp1.insert(pair<int, int>(5, 50));
    printMap(mp1);

    //查找
    map<int, int>::iterator it = mp1.find(3);
    if (it != mp1.end())
    {
        cout << "查到了元素 key=" << it->first << " value=" << it->second << endl;
    }
    else
    {
        cout << "未找到元素" << endl;
    }
    //输出结果:查到了元素 key=3 value=30

    //统计
    int num = mp1.count(4);
    cout << "mp1中的个数:" << num << endl;
    //输出结果:mp1中的个数:1
    //map不允许插入重复的key元素,count统计而言 结果要么是0 要么是1
    //mulyimap的count统计可能大于1
}
int main()
{
    test01();
    system("pause");
    return 0;
}

总结:

查找--find      (返回的是迭代器)

统计--count   (对于map,结果为o或者1)

33.6map容器排序

学习目标:map容器默认排序规则为 按照key值进行 从大到小排序,掌握如何改变排序规则

主要技术点:

利用仿函数,改变排序规则

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

class MyCompare
{
public:
    bool operator()(int v1, int v2)const//vs2019需要在仿函数后加const
    {
        return v1 > v2;
    }
};
void printMap(const map<int, int, MyCompare>& m)
{
    for (map<int, int, MyCompare>::const_iterator it = m.begin(); it != m.end(); it++)
    {
        cout << "key=" << (*it).first << " value=" << (*it).second << endl;
    }
}
void test01()
{
    //map<int,int,a>mp1中a的位置默认有一个less排序规则,让其从小到大排序
    map<int, int, MyCompare > mp1;
    mp1.insert(pair<int, int>(1, 10));
    mp1.insert(pair<int, int>(3, 30));
    mp1.insert(pair<int, int>(2, 20));
    mp1.insert(pair<int, int>(4, 40));
    mp1.insert(pair<int, int>(5, 50));
    printMap(mp1);
}
int main()
{
    test01();
    system("pause");
    return 0;
}

输出结果: 

key=5 value=50
key=4 value=40
key=3 value=30
key=2 value=20
key=1 value=10

总结:

利用仿函数可以制定map容器的排序规则

对于自定义数据类型,map必须要指定排序规则,同set容器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值