简体   繁体   English

map 是否有可能在 C++ 中有两个映射作为键和值?

[英]Is it possible for a map to have two maps as a key and value in C++?

Hi I'm trying to have a map of a sort like map<map<string,string>,map<int,int>>.嗨,我正在尝试使用类似于 map<map<string,string>,map<int,int>> 的 map。 Is it possible to have such a map in C++..?是否有可能在 C++.. 中有这样的 map? Or is the concept incorrect..?还是这个概念不正确..? Using this method to have something like a cricket players status - name, designation, runs and wickets.使用这种方法可以获得类似板球运动员的状态 - 姓名、名称、跑步和三柱门。 The code I've pasted is something I tried, it's incorrect.我粘贴的代码是我尝试过的,它不正确。

map<map<string,string>,map<int,int>> m;
    map<map<string,string>,map<int,int>>::iterator itr;
    map<int,int>::iterator ptr;
    m.insert(make_pair("string1","string2"),map<int,int>());
    m["string1"].insert(make_pair(10,20));
    
    for(itr = m.begin();itr!=m.end();itr++) {
        for (ptr = itr->second.begin(); ptr != itr->second.end(); ptr++) {
            cout << "First key is " << itr->first<< " And second key is " << ptr->first<< " And value is " << ptr->second << endl;
        }
    }

All feedback is greatly appreciated, thanks!非常感谢所有反馈,谢谢!

Is it possible for a map to have two maps as a key and value in C++? map 是否有可能在 C++ 中有两个映射作为键和值?

Yes, it is.是的。 There aren't many restrictions on the value type.对值类型没有太多限制。 Primary restriction on the key is that it must be orderable by the provided comparator ( std::less<Key> by default).对键的主要限制是它必须可由提供的比较器排序(默认为std::less<Key> )。 std::map does satisfy those requirements given appropriate key and value types.给定适当的键和值类型, std::map确实满足这些要求。

 m.insert(make_pair("string1","string2"),map<int,int>());

This isn't correct.这是不正确的。 You cannot pass the value as a separate argument to insert .您不能将该值作为单独的参数传递给insert Furthermore you cannot initialise the key that is a map using a pair.此外,您不能使用一对初始化作为 map 的密钥。 Here is a correct example:这是一个正确的例子:

std::map<std::string, std::string> key {
    {"string1", "string2"},
};
// not needed since operator[] will insert a value initialised element
// m.emplace(key, std::map<int,int>{});
 m["string1"].insert(make_pair(10, 20));

This isn't correct.这是不正确的。 The key of the map is another map, and a string cannot be compared to a map, so this lookup cannot work. map 的键是另一个 map,字符串无法与 map 进行比较,因此无法进行此查找。 In this corrected example:在这个更正的例子中:

m[key].emplace(10, 20);
 cout << "First key is " << itr->first<< " And second key is " << ptr->first<< " And value is " << ptr->second << endl;

This won't work since you cannot insert a map (that is the key) into a character stream.这将不起作用,因为您无法将 map(即密钥)插入字符 stream。 You could write something like this for example:例如,您可以编写如下内容:

for (auto& [key, value] : m) {
    std::cout << "key: ";
    for (auto& [key_key, key_value] : key) {
        std::cout
            << "key of key is " << key_key
            << " value of key is " << key_value
            << ", "
        ;
    }
    std::cout << "\nvalue: ";
    for (auto& [value_key, value_value] : value) {
        std::cout
            << "key of value is " << value_key
            << " value of value " << value_value
             << ", "
        ;
    }
    std::cout << "\n\n";
}

However, it's quite rare for map to be a useful key of a map, and considering your mistaken use of your map, I suspect that you actually need the key to be a string rather than a map. However, it's quite rare for map to be a useful key of a map, and considering your mistaken use of your map, I suspect that you actually need the key to be a string rather than a map.

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

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