Search code examples
c++arraysdictionary2d

How to make a 2D map similar to a 2D array


Is it possible to make a 2d map?

Like this:

map< int, int, string> testMap;

And filling the values would be like:

testMap[1][3] = "Hello";

Solution

  • You can nest two maps:

    #include <iostream>
    #include <map>
    #include <string>
    
    int main()
    {
        std::map<int,std::map<int,std::string>> m;
    
        m[1][3] = "Hello";
    
        std::cout << m[1][3] << std::endl;
    
        return 0;
    }