STL-map/multimap

#include <iostream>
#include <map>
#include <algorithm>

using namespace std;

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

void test01()
{
    //构造
    map<int, int> m1{{1, 10}, {2, 20}, {3, 30}};
    printMap(m1);
    map<int, int> m2(m1);
    printMap(m2);
    map<int, int> m3 = m1;
    printMap(m3);
}

void test02()
{
    map<int, int> m1{{1, 10}, {2, 20}, {3, 30}};
    if (m1.empty())
    {
        cout << "m1为空!" << endl;
    }
    else
    {
        cout << "m1不为空,大小为" << m1.size() << endl;
        for_each(m1.begin(), m1.end(), [&](const pair<int, int> &p) {
            cout << "key = " << p.first << ", value = " << p.second << endl;
        });
    }

    //交换
    map<int, int> m2{{4, 40}, {5, 50}, {6, 60}};
    m1.swap(m2);
    cout << "交换后" << endl;
    printMap(m1);
}

void test03()
{
    map<int, char> m1;
    m1.insert(pair<int, char>(0, 'a'));
    m1.insert(make_pair(1, 'b'));
    m1.insert(map<int, char>::value_type(2, 'c'));
    m1[3] = 'd';
    for_each(m1.begin(), m1.end(), [&](const pair<int, char> p) {
        cout << "key = " << p.first << ", value = " << p.second << endl;
    });

    //删除
    m1.erase(m1.begin());
    m1.erase(2);
    for_each(m1.begin(), m1.end(), [&](const pair<int, char> p) {
        cout << "key = " << p.first << ", value = " << p.second << endl;
    });

    map<int, char>::iterator pos = m1.find(3);
    if (pos == m1.end())
    {
        cout << "未找到元素" << endl;
    }
    else
    {
        int num = m1.count(3);
        cout << "找到了元素key = " << pos->first << ", value = " << pos->second \
             << ", 该元素有" << num << "个" << endl;
    }

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

int main(int argc, char const *argv[])
{
    /*1. map中所有元素都是pair,pair中第一个元素是key(键值),起到索引作用,第二个元素是value(实值)
    2. 所有元素都会根据元素的键值自动排序
    3. 优点:可以根据key值快速找到value值
    4. map不允许容器中有重复的key值元素,multimap允许容器中有重复key值元素*/

    //构造
    test01();

    //大小和交换
    test02();

    //插入和删除
    test03();

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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类似,只是它允许多个具有相同键的元素存在。 这些关联容器在查找和插入操作上具有较高的效率,特别适用于需要根据键进行快速查找的场景。在实际应用中,你可以根据自己的需求选择适合的容器类型。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值