C++ 等效于 Python 字典

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27674009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:07:07  来源:igfitidea点击:

C++ equivalent of Python dictionaries

pythonc++

提问by Jenny Calisay

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) :

我目前正在使用 AI 制作井字棋程序,但在翻译这行代码 (python) 时遇到了一些麻烦:

RANKS = dict([(4,3),                       # center  = 3
              (0,2),(2,2),(6,2),(8,2),     # corners = 2
              (1,1),(3,1),(5,1),(7,1)])    # sides   = 1

into C++

到 C++

any suggestions?

有什么建议?

采纳答案by juanchopanza

The closest match in C++ would be an std::unordered_map<int, int>. This is a hash table mapping intkeys to intvalues.

C++ 中最接近的匹配是std::unordered_map<int, int>. 这是一个将int键映射到int值的哈希表。

#include <unordered_map>


std::unordered_map<int, int> RANKS = {
        { 4, 3 },
        { 0, 2 }, { 2, 2 }, { 6, 2 }, { 8, 2 },
        { 1, 1 }, { 3, 1 }, { 5, 1 }, { 7, 1 }
};

You can access elements using operator[], for example

operator[]例如,您可以使用 访问元素

std::cout << RANKS[0] << std::endl; // prints "2"

Note that the C++ standard library also has the std::mapclass template, which allows you to create a similar but orderedlook-up table std::map<int, int>, with logarithmic look-up and insertion complexity. But python dicts are hash tables, so unordered_mapis a closer match in terms of behaviour.

请注意,C++ 标准库也有std::map类模板,它允许您创建一个类似但有序的查找表std::map<int, int>,具有对数查找和插入复杂性。但是 pythondict是哈希表,因此unordered_map在行为方面更接近。

回答by Captain Obvlious

In C++ this would be a std::unordered_map

在 C++ 中,这将是一个 std::unordered_map

#include <unordered_map>

std::unordered_map<int, int> dict
{
    {
        { 4, 3 },
        { 0, 2 }, { 2, 2 }, { 6, 2 }, { 8, 2 },
        { 1, 1 }, { 3, 1 }, { 5, 1 }, { 7, 1 }
    }
};

回答by Pradhan

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

Python 的 C++ 等价物dictstd::map. 要使用类似的语法初始化地图,请执行以下操作:

std::map<int,int> 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.

请注意,这需要 C++11。

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

如果您不能使用 C++11,请转到map_list_ofBoost.Assign。他们页面上的例子是:

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

回答by Galik

Although a "language equivalent" might me something like an std::unordered_mapyour use case may be more efficiently served with a straight array:

尽管“语言等效”可能类似于std::unordered_map您的用例,但使用直数组可能更有效:

int RANKS[] = {2, 1, 2, 1, 3, 1, 2, 1, 2};

回答by Jerry Coffin

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.

您可以为此使用 map 或 unordered_map(并且它们可以正常工作),但鉴于您的键是一组密集的整数(即从 0 到 N 的所有整数),有更好的选择。

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

我可能会用一个std::array代替。它看起来像这样:

std::array <char, 9> 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.

这提供了几乎相同的语法和可观察的行为,但通常会节省相当多的内存和 CPU 时间。

回答by dlna

The "language equivalent" is:

“语言等效”是:

#include <unordered_map>


std::unordered_map<int, int> RANKS = {
        { 4, 3 },
        { 0, 2 }, { 2, 2 }, { 6, 2 }, { 8, 2 },
        { 1, 1 }, { 3, 1 }, { 5, 1 }, { 7, 1 }
};

In case your variable's name was UPPERCASE is because it was a constant:

如果您的变量名称是大写是因为它是一个常量:

#include <unordered_map>


const std::unordered_map<int, int> RANKS = {
        { 4, 3 },
        { 0, 2 }, { 2, 2 }, { 6, 2 }, { 8, 2 },
        { 1, 1 }, { 3, 1 }, { 5, 1 }, { 7, 1 }
};