1776. 珅泽教育CSP-J第一轮模拟考第八套 第 35 题

珅泽教育CSP-J第一轮模拟考第八套 第 35 题

二、阅读程序·第三题(选择题)

struct hash_map
{
    const int max_size = 32767;
    struct node
    {
        int key;
        int value;
        int next;
    }
    map[max_size * 4];

    int size = 0;

    int hash[max_size] = {0};

    int& operator[] (int key)
    {
        int code = key % max_size;

        int addr = hash[code];
        while (addr > 0)
        {
            if (map[addr].key == key)
                return map[addr].value;
            else
                addr = map[addr].next;
        }

        ++size;
        map[size].key = key;
        map[size].value = 0;
        map[size].next = hash[code];
        hash[code] = size;

        return map[size].value;
    }
};

void test(int q)
{
    hash_map map;
    while (q > 0)
    {
        --q;

        char op;
        std::cin >> op;
        if (op == '+')
        {
            int key, value;
            std::cin >> key >> value;
            map[key] = value;
        }
        if (op == '?')
        {
            int key;
            std::cin >> key;
            std::cout << map[key] << "\n";
        }
    }
}

以下说法正确的是( )。

{{ select(1) }}

  • 当 value 改为 std::string 类型时,需要对程序进行较大的改动
  • 当 key 改为 std::string 类型时,需要对程序进行较大的改动
  • hash_map 无法提供删除功能
  • hash_map 处理的 key 数值大小不能超过 max_size