C++ equivalent of Python dictionaries

后端 未结 5 943
后悔当初
后悔当初 2021-02-14 06:42

I\'m currently making a tic-tac-toe program with AI and i\'m having a bit of a trouble translating this line of code (python) :

RANKS = dict([(4,3),                      


        
5条回答
  •  不要未来只要你来
    2021-02-14 07:06

    The C++ equivalent of Python's dict is std::map. To initialize a map using a similar syntax, do this:

    std::map myMap = {{4,3},                       # center  = 3
                               {0,2},{2,2},{6,2},{8,2},     # corners = 2
                               {1,1},{3,1},{5,1},{7,1}};    # sides   = 1
    

    Note that this needs C++11.

    If you cannot use C++11, turn to map_list_of in Boost.Assign. The example from their page is:

    using namespace boost::assign; // bring 'map_list_of()' into scope
    std::map next = map_list_of(1,2)(2,3)(3,4)(4,5)(5,6);
    

提交回复
热议问题