简体   繁体   English

初始化二维 map c++

[英]Initialising a 2d map c++

I have been thinking lately about 2d structures (of integers) in which their sizes can grow dynamically.我最近一直在考虑二维结构(整数),它们的大小可以动态增长。 I came across 'map' which I think that it meets my following need: Essentially, what I want is to check whether a random entry has been initialized or not (do something if it has not been initialized yet).我遇到了“地图”,我认为它满足了我的以下需求:本质上,我想要检查一个随机条目是否已初始化(如果尚未初始化,请执行某些操作)。

int main(){ 
   int int1, int2;
   std::map<int,std::map<int,int>> my_map;
   cout << "Enter two integers separated by space: ";
   cin >> int1 >> int2;
   if(has my_map[int1][int2] not been initialized){
      do something
   }
}

I am hoping that such functionality is available with C++.我希望 C++ 可以使用这样的功能。

If what you mean by checking whether an entry "has been initialized" is checking that a pair of keys has a value assigned to them -- one key for the outer map and one for the inner map -- you can test contents of a map of maps as below:如果您通过检查条目“已初始化”的意思是检查一对键是否具有分配给它们的值——一个用于外部 map 的键和一个用于内部 map 的键——您可以测试 Z1D78DC8ED41214E50AEFE2441 的内容地图如下:

bool contains_keys(const std::map<int, std::map<int, int>>& map_of_maps, int key1, int key2) {
    auto iter = map_of_maps.find(key1);
    if (iter == map_of_maps.end()) {
        return false;
    }
    return iter->second.find(key2) != iter->second.end();
}

However, I question whether a map of maps is really what you want.但是,我质疑 map 地图是否真的是你想要的。 If what you want is just a mapping from two keys to a single value, a more direct and space efficient implementation is to use an std::map with an std::tuple or std::pair as the key type.如果您想要的只是从两个键到单个值的映射,那么更直接和节省空间的实现是使用 std::map 和 std::tuple 或 std::pair 作为键类型。

Tuples version below.下面的元组版本。

#include <map>
#include <tuple>
#include <iostream>

int main()
{
    std::map<std::tuple<int, int>, int> map_of_pairs;
    map_of_pairs[{42, 17}] = 3;

    bool contains_42_17 = (map_of_pairs.find({ 42, 17 }) != map_of_pairs.end());
    bool contains_67_23 = (map_of_pairs.find({ 67, 23 }) != map_of_pairs.end());

    std::cout << ((contains_42_17) ? "yes\n" : "no\n");
    std::cout << ((contains_67_23) ? "yes\n" : "no\n");
}

Also unless you actually need the above to be ordered, consider an unordered_map.此外,除非您确实需要订购上述内容,否则请考虑使用 unordered_map。

You could write a function that first checks the outer map for key1 , then if an inner map exists with that key, check the inner map for key2 . You could write a function that first checks the outer map for key1 , then if an inner map exists with that key, check the inner map for key2 .

bool nested_contains(std::map<int,std::map<int,int>> const& my_map, int key1, int key2)
{
    auto itOuter = my_map.find(key1);
    if (itOuter == my_map.end())
        return false;
        
    return itOuter->second.contains(key2);
}

Then use it like然后像这样使用它

int main()
{ 
   int int1, int2;
   std::map<int,std::map<int,int>> my_map;
   std::cout << "Enter two integers separated by space: ";
   std::cin >> int1 >> int2;
   if(nested_contains(my_map, int1, int2)){
      // do something
   }
}

You can quickly do it creating a custom function using the find method.您可以使用 find 方法快速创建自定义 function。 In that case the algorithm is suitable for being generic.在这种情况下,该算法适合通用。

template<typename T>
bool contains_keys_cpp_20(std::map<T, std::map<T, T>> const& nested_map, T key1, T key2)
{
    auto pair = nested_map.find(key1);
    return (pair != nested_map.end()) && (pair->second.contains(key2));
}

template<typename T>
bool contains_keys_cpp_17(std::map<T, std::map<T, T>> const& nested_map, T key1, T key2)
{
    auto pair = nested_map.find(key1);
    return (pair != nested_map.end()) && (pair->second.find(key2) != pair->second.end());
}

int main()
{
    {
        std::map<int, std::map<int, int>> my_map;
        my_map[1] = { { 2, 1} };
        bool result = contains_keys_cpp_17(my_map, 3, 2);
        // false
    }

    {
        std::map<int, std::map<int, int>> my_map;
        my_map[3] = { { 2, 1} };
        bool result = contains_keys_cpp_17(my_map, 3, 2);
        // true
    }

    {
        std::map<char, std::map<char, char>> my_map;
        my_map['3'] = { { '2', '1'} };
        bool result = contains_keys_cpp_17(my_map, '3', '2');
        // true
    }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM