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

本文详细介绍了C++标准库中的map和multimap容器的使用方法及注意事项,包括自定义键值比较、插入、查找、删除等操作,并通过代码示例展示了这两种容器的基本用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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



C++ STL中的mapmultimap是关联容器,用于存储键值对(key-value pairs),其中每个键(key)唯一对应一个值(value)。 map是一个有序容器,根据键的大小进行自动排序,默认按照键的升序进行排序。每个键只能在map中出现一次,如果尝试插入具有相同键的元素,新元素将替代旧元素。 multimap也是一个有序容器,与map不同的是,它允许多个具有相同键的元素存在。多个具有相同键的元素将按照插入的顺序进行存储,而不会自动排序。 这两个容器都提供了一系列的操作函数,如insert、erase、find等,用于插入、删除和查找元素。 以下是一个使用map的简单示例: ```cpp #include <iostream> #include <map> int main() { std::map<std::string, int> scores; scores.insert(std::make_pair("Alice", 90)); scores.insert(std::make_pair("Bob", 80)); scores.insert(std::make_pair("Charlie", 70)); // 查找并输出Bob的分数 std::cout << "Bob's score: " << scores["Bob"] << std::endl; // 遍历并输出所有键值对 for (const auto& pair : scores) { std::cout << pair.first << ": " << pair.second << std::endl; } return 0; } ``` 上述示例中,我们创建了一个存储string类型键和int类型值的map容器scores。通过insert函数依次插入了三个键值对。然后我们通过scores["Bob"]来获取Bob的分数,并输出结果为80。 接着我们使用范围-based for循环遍历map中的所有键值对,并输出每个键值对的键和值。 multimap用法map类似,只是它允许多个具有相同键的元素存在。 这些关联容器在查找和插入操作上具有较高的效率,特别适用于需要根据键进行快速查找的场景。在实际应用中,你可以根据自己的需求选择适合的容器类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值