how to use stl::map as two dimension array

前端 未结 3 651
感动是毒
感动是毒 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:33

    An alternative solution to Andrew Stein's which plays nicer with the rest of STL is to simply use

    typedef std::map, int > AMapT;
    AMapT mymap;
    mymap[std::make_pair(2, 4)] = 10;
    ...
    AMapT::iterator f = mymap.find(std::make_pair(3, 5));
    

    For example, with this way you don't need to chain two calls to map::find to search for a single value.

提交回复
热议问题