C++ Map/Multimap 常见用法全解(代码版)

c++map容器提供一个键值对(key/value)容器,map与multimap差别仅仅在于multimap允许一个键对应多个值。对于迭代器来说,可以修改实值,而不能修改key。map会根据key自动排序。map底层是使用红黑树。

Map代码实例:

class sst{
public:
    int x;
    int y;
    sst(int a, int b):x(a), y(b){}
};

bool operator<(const sst& a, const sst& b)
{
    return a.x < b.x;       //代表map中key是sst中的x。
}

void map_test()     //还有一种unordered_map。区别就是不会自动排序。
{
    map<sst ,int> mp;
    //插入
    for (int i = 1; i < 10; i++)
        mp[sst(i, i + 1)] = i * 5;
    
    mp.insert(make_pair(sst(2, 5), 50));                //当map中已经有sst.x=2的项。如果再insert sst.x=2的值就会忽略。
    
    for (auto it = mp.begin(); it != mp.end(); it++)    //1-2-5  2-3-10  3-4-15  4-5-20  5-6-25  6-7-30  7-8-35  8-9-40  9-10-45
        cout<<it->first.x<<"-"<<it->first.y<<"-"<<it->second<<"  ";
    cout<<endl;
    
    //使用
    cout<<mp[sst(2, 5)]<<endl;                          //10 当map有sst.x = 2的项(不管y对没对上),返回value值
    cout<<mp[sst(10, 11)]<<endl;                        //0  当map中没有sst.x=10的项,这个语句会添加 mp[sst(10, 11)] = 0;
    
    for (auto it = mp.begin(); it != mp.end(); it++)    //1-2-5  2-3-10  3-4-15  4-5-20  5-6-25  6-7-30  7-8-35  8-9-40  9-10-45  10-11-0
        cout<<it->first.x<<"-"<<it->first.y<<"-"<<it->second<<"  ";
    cout<<endl;
    
    //删除
    mp.erase(sst(2, 0));
    mp.erase(mp.begin());

    for (auto it : mp)                                  //3-4-15  4-5-20  5-6-25  6-7-30  7-8-35  8-9-40  9-10-45  10-11-0
        cout<<it.first.x<<"-"<<it.first.y<<"-"<<it.second<<"  ";
    cout<<endl;

    //其他
    cout<<mp.lower_bound(sst(5, 5))->second<<endl;     //25 返回第一个sst.x比 5 大于等于的 迭代器
    cout<<mp.upper_bound(sst(5, 5))->second<<endl;     //30 返回第一个sst.x比 5 大于 的 迭代器

    auto ret = mp.equal_range(sst(6, 6));              //找key等于25的迭代器范围。[ret.first, ret.second)
    cout<<ret.first->first.x<<" "<<ret.first->second<<endl;     //6 30
    cout<<ret.second->first.x<<" "<<ret.second->second<<endl;   //7 35

    mp.size();
    cout<<mp.find(sst(3, 33))->first.y<<endl;   //4 找的是sst.x = 3的迭代器,和sst.y无关
    mp.empty();
    mp.clear();
}

mutlimap代码实例:

class msst{
public:
    int x;
    int y;
    msst(int a, int b):x(a), y(b){}
};

bool operator<(const msst& a, const msst& b)
{
    return a.x < b.x;       //代表map中key是sst中的x。
}


void multimap_test()
{
    multimap<msst ,int> mp;
    //插入
    for (int i = 1; i < 10; i++)
        mp.insert(make_pair(msst(i, i + 1), i * 5));
    
    mp.insert(make_pair(msst(2, 5), 50));               //在multimap中就可以有多个 sst.x=2 的项。
    mp.insert(make_pair(msst(6, 6), 6));
    
    for (auto it = mp.begin(); it != mp.end(); it++)    //1-2-5  2-3-10  2-5-50  3-4-15  4-5-20  5-6-25  6-7-30  6-6-6  7-8-35  8-9-40  9-10-45
        cout<<it->first.x<<"-"<<it->first.y<<"-"<<it->second<<"  ";
    cout<<endl;
    
    //使用
    //cout<<mp[msst(2, 5)]<<endl;                          //multimap不能够这样读取
    
    //删除
    mp.erase(msst(2, 0));               //删除所有sst.x = 2的项
    mp.erase(mp.begin());               //mp.erase(mp.begin() + 4)这样是不行的
    
    for (auto it : mp)                                  //3-4-15  4-5-20  5-6-25  6-7-30  6-6-6  7-8-35  8-9-40  9-10-45
        cout<<it.first.x<<"-"<<it.first.y<<"-"<<it.second<<"  ";
    cout<<endl;
    
    //其他
    cout<<mp.lower_bound(msst(6, 5))->second<<endl;     //30 返回第一个sst.x比 6 大于等于的 迭代器
    cout<<mp.upper_bound(msst(6, 5))->second<<endl;     //35 返回第一个sst.x比 6 大于 的 迭代器
    
    auto ret = mp.equal_range(msst(6, 6));              //找key等于25的迭代器范围。[ret.first, ret.second)
    cout<<ret.first->first.x<<" "<<ret.first->second<<endl;     //6 30
    cout<<ret.second->first.x<<" "<<ret.second->second<<endl;   //7 35
    
    mp.size();
    cout<<mp.find(msst(6, 33))->first.y<<endl;   //7 找的是sst.x = 6的第一个迭代器,和sst.y无关
    mp.empty();
    mp.clear();
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值