C++ equivalent of Python dictionaries

后端 未结 5 801
后悔当初
后悔当初 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 06:51

    You could use a map or unordered_map for this (and they'd work fine) but given that your keys are a dense set of integers (I.e. all the integers from 0 to N) there are better choices.

    I'd probably use an std::array instead. It would look something like this:

    std::array  vals = { 2, 1, 2, 1, 3, 1, 2, 1, 2 };
    

    This gives pretty much the same syntax and observable behavior, but will typically save quite a bit of memory and probably CPU time as well.

提交回复
热议问题