C ++是否等同于Python字典列表? [英] C++ Equivalent of Python List of Dictionaries?

查看:90
本文介绍了C ++是否等同于Python字典列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python词典列表,其中包含有关神经网络各层的信息。每个词典可以有任意数量的条目,包括更多的词典。

I have a Python list of dictionaries that contain information about individual layers of a neural network. Each dictionary can have any number of entries, including more dictionaries.

layer_config = [
 {'conv_layer': {
     'filter_size' : 5,
     'stride' : 1,
     'num_filters' : 20}},
 {'pool_layer': {
     'poolsize' : (2,2)}},
 {'fc_layer': {
     'num_output' : 30}},
 {'final_layer': {
     'num_classes' : 10}}
]

是C ++嵌套映射是实现此目标的最佳方法,还是存在另一种可能更好的数据结构?

I am converting the program into C++ and need to find a way to organize this information in a similar manner. Are C++ nested maps the best way to accomplish this, or is there another data structure out there that might be a better alternative?

推荐答案

在C ++中,要使用嵌套映射解决此问题,每个映射都必须具有相同的类型。如果您创建了地图地图,则所有子地图都必须保存相同类型的信息(例如字符串和整数),并且根据键,您似乎在字典中保存了不同的信息(即,您有一个对(2,2),位于键 poolsize处,在其他位置您具有整数)。 C ++实现此目的的方法可能是创建一个保存此信息的结构或类。例如,您可以为conv_layer,pool_layer等创建带有四个映射的结构。从您的示例看来,您的数据结构似乎只需要一个映射用于conv_layer,并需要一堆对用于所有其他变量。如果是这种情况,则可以对数据结构使用类似的内容:

In C++, to use nested maps for this problem, each map would have to be of the same type. If you created a map of maps, the submaps would all have to hold the same type of information (like strings and ints), and it appears that you have different information being held in your dictionary depending on the key, (i.e you have a pair (2,2) at key "poolsize", where elsewhere you have integers). The C++ way to do this could be to create a struct or class which holds this information. For instance, you could create a struct with four maps for your conv_layer, pool_layer and so on. From your example, it looks like your data structure only needs one map for conv_layer, and a bunch of pairs for all of the other variables. If this is the case, you could use something like this for your data structure:

struct layer_config{
        std::map<std::string, int> conv_layer;
        std::pair<std::string, std::pair<int, int>> pool_layer;
        std::pair<std::string, int> fc_layer;
        std::pair<std::string, int> final_layer;
};

这篇关于C ++是否等同于Python字典列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆