how to use stl::map as two dimension array

前端 未结 3 654
感动是毒
感动是毒 2020-12-16 07:09

Could you let us know how to use stl:map as two dimension array? I wanted to access the individual elements as like mymap[i][j] where I do not know beforehand what the value

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 07:30

    You can do

    std::map > mymap;
    

    For example:

    #include 
    #include 
    
    int main() 
    {
        std::map > mymap;
    
        mymap[9][2] = 7;
        std::cout << mymap[9][2] << std::endl;
    
        if (mymap.find(9) != mymap.end() && mymap[9].find(2) != mymap[9].end()) {
            std::cout << "My map contains a value for [9][2]" << std::endl;
        } else {
            std::cout << "My map does not contain a value for [9][2]" << std::endl;
        }
    
        return 0;
    }
    

    prints 7 on the standard output, followed by "My map contains a value for [9][2]".

提交回复
热议问题