C++练习题:STL容器map的应用

第一部分:考点与作答区

考点:

  • STL容器的概念
  • map容器的特点
  • map容器的常用操作

作答区: 编写一个C++程序,完成以下要求:

  1. 使用 <map> 头文件声明一个 map 对象。
  2. map 对象中插入键值对。
  3. 打印 map 对象中的所有键值对。
  4. 根据键获取 map 对象中的值。
  5. 根据值获取 map 对象中的键。
  6. 删除 map 对象中的键值对。
  7. 获取 map 对象的大小。

请在下方空白处编写代码:

// 在此处编写代码
第二部分:解题思路与答案

解题思路:

  1. 首先,包含 <map> 头文件,声明一个 map 对象。
  2. 使用 insert() 函数在 map 对象中插入键值对。
  3. 使用 begin()end() 函数获取迭代器,使用迭代器访问 map 对象中的键值对。
  4. 使用 at()operator[] 函数根据键获取 map 对象中的值。
  5. 使用 at()operator[] 函数根据值获取 map 对象中的键。
  6. 使用 erase() 函数删除 map 对象中的键值对。
  7. 使用 size() 函数获取 map 对象的大小。

答案:

#include <iostream>
#include <map>

int main() {
    // 声明 map 对象
    std::map<int, std::string> intStringMap;

    // 插入键值对
    intStringMap[1] = "One";
    intStringMap[2] = "Two";
    intStringMap[3] = "Three";

    // 打印键值对
    for (const auto &pair : intStringMap) {
        std::cout << pair.first << ": " << pair.second << std::endl;
    }

    // 根据键获取值
    std::cout << "Value for key 2: " << intStringMap.at(2) << std::endl;

    // 根据值获取键
    std::cout << "Key for value Two: " << intStringMap.at(std::string("Two")) << std::endl;

    // 删除键值对
    intStringMap.erase(2);

    // 再次打印键值对
    for (const auto &pair : intStringMap) {
        std::cout << pair.first << ": " << pair.second << std::endl;
    }

    // 获取大小
    std::cout << "Size of map: " << intStringMap.size() << std::endl;

    return 0;
}

在这段代码中,我们首先包含 <map> 头文件,声明一个 map 对象,并使用 insert() 函数插入键值对。然后,我们使用迭代器访问 map 对象中的键值对,并使用 at()operator[] 函数根据键或值获取对应的值或键。我们使用 erase() 函数删除键值对,并再次打印键值对。最后,我们使用 size() 函数获取 map 对象的大小。通过这种方式,我们展示了STL容器map的基本用法。

第三部分:扩展考点与扩展作答区

扩展考点:

  • map容器的排序
  • map容器的遍历

扩展作答区: 在原有程序的基础上,增加以下功能:

  1. map 对象中的键进行排序。
  2. 遍历 map 对象,并打印出键值对。

请在下方空白处编写代码:

// 在此处编写代码
第四部分:扩展解答思路和答案

扩展解答思路:

  1. map 对象中的键进行排序。这可以通过调用 map 容器的 sort() 函数来实现,该函数会就地对 map 容器进行排序。
  2. 遍历 map 对象,并打印出键值对。这可以通过使用迭代器来遍历 map 容器,并打印出每个键值对。

答案:

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

int main() {
    // 声明 map 对象
    std::map<int, std::string> intStringMap;

    // 插入键值对
    intStringMap[1] = "One";
    intStringMap[2] = "Two";
    intStringMap[3] = "Three";

    // 打印键值对
    for (const auto &pair : intStringMap) {
        std::cout << pair.first << ": " << pair.second << std::endl;
    }

    // 对键进行排序
    std::map<int, std::string>::iterator it = intStringMap.begin();
    while (it != intStringMap.end()) {
        std::map<int, std::string>::iterator nextIt = it;
        nextIt++;
        std::map<int, std::string>::iterator lastIt = it;
        lastIt++;
        if (lastIt != intStringMap.end()) {
            intStringMap.erase(lastIt);
        }
        if (nextIt != intStringMap.end()) {
            intStringMap.erase(nextIt);
        }
        it = intStringMap.begin();
    }

    // 再次打印排序后的键值对
    for (const auto &pair : intStringMap) {
        std::cout << pair.first << ": " << pair.second << std::endl;
    }

    // 获取大小
    std::cout << "Size of map: " << intStringMap.size() << std::endl;

    return 0;

www.zblzrcw.com
www.zbzczpw.com
www.zbhtrcw.com
www.zbgqrcw.com
www.zbyyrcw.com
}

在这段代码中,我们首先包含 <map> 头文件,声明一个 map 对象,并使用 insert() 函数插入键值对。然后,我们打印出键值对。接着,我们对 map 对象中的键进行排序,并再次打印排序后的键值对。通过这种方式,我们展示了STL容器map的排序和遍历操作。 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值