繁体   English   中英

将多种类型存储为 C++ 字典中的值?

[英]Store multiple types as values in C++ dictionary?

我想编写一个行为几乎等同于 Python 字典的 C++ 对象。 C++ 的std::mapstd::unordered_map包含 Python 字典已有的一些功能,但缺少最重要的功能之一,即能够添加任意对象和类型。 即使不可能,您离实现 Python 字典的功能还有多远?

之前的一些问题( 此处此处)无法处理向字典添加多种类型的问题。

例如,我希望能够在 C++ 中做这样的事情:

my_dict = {'first': 1,
           'second': "string",
           'third': 3.5,
           'fourth': my_object}
my_dict['fifth'] = list([1, 2, 3])

我能想到的最好的解决方案是使用指向被重新解释的数据的void指针,或者某种具有某些类型限制的运行时多态性?

我能想到的最佳解决方案是使用指向重新解释的数据的空指针,或者某种具有某些类型限制的运行时多态性?

在我看来,现代 C++ 中的许多空指针和对指针的许多重新解释应该是一个糟糕设计的信号。 我相信多态性将是一种方法。

此外,如果您想要使用固定数量的类型,请考虑使用 C++17 的std::anystd::variant ,这是一个更现代的union

#include <iostream>
#include <map>
#include <string>
#include <variant>

typedef std::map<std::variant<int, std::string>, std::variant<int, std::string>> Dict;

int main(){
    Dict d;
    d["key"] = 1;
    d[5] = "woah";
    std::cout << std::get<int>(d["key"]) << std::endl; // prints 1

    // edit: print all the keys in d
    for(auto& k_v: d){
        std::visit(
            [](auto& value){std::cout << value << ' ';},
            k_v.first // k_v.second to print all values in d
        );
    }

}

上面的示例在某些用例中可能很有用。

还要查看 json 对象是如何在任何 C++ json 库中实现的。 它可能会有所帮助。

编辑:我添加了一个std::visit示例,它遍历字典中的键。

我正在寻找类似的解决方案,将我的python实验的参数硬编码到c++ 我发现std'17 variant模块非常有用。

#include <iostream>
#include <map>
#include <variant>
#include <vector>
#include <string>

int main(){
    typedef std::map<std::variant<std::string, int>, std::variant<int, double, float, std::string>> Dict;

    std::vector<Dict> params = {
        {
            {"label", "Labs"},
            {"dir", "/media/sf_Data/"},
            {"num_frames", 4},
            {"scale_factor", 1.0},
            {5, "my number five"},
        }, // Dict 0
        {
            {"label", "Airplanes"},
            {"dir", "/media/m_result/"},
            {"num_frames", 5},
            {"scale_factor", 0.5},
            {5, "number five"},
        } // Dict 1
    };

    int idx = 1;
    std::string label   = std::get<std::string>(params[idx]["label"]);
    std::string folder  = std::get<std::string>(params[idx]["dir"]);
    int num_frames      = std::get<int>(params[idx]["num_frames"]);
    double scf          = std::get<double>(params[idx]["scale_factor"]);
    std::string nbstr   = std::get<std::string>(params[idx][5]);
    
    std::cout << label << std::endl;
    std::cout << folder << std::endl;
    std::cout << num_frames << std::endl;
    std::cout << scf << std::endl;
    std::cout << nbstr << std::endl;

    return 0;
}

结果:

Airplanes
/media/m_result/
5
0.5
number five

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM