在 C++ 中返回字符串和不同向量数据类型的映射 [英] Returning a map of string and different vector datatypes in C++

查看:19
本文介绍了在 C++ 中返回字符串和不同向量数据类型的映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道以前有人问过这样的问题,但我仍在努力寻找答案.

I know questions like this has been asked before, but I am still struggling to find an answer.

我试图返回一个stringvectormap方法;我有两个向量 std::vector样本std::vectorsample_names

I am trying to return a method of map of string and vector; I have two vectors std::vector<Eigen::MatrixXd> samples and std::vector<std::string> sample_names

这是我目前所做的:

std::map<std::string, std::vector> FileReader::get_data() {
    std::map<std::string, std::vector> content;

    std::vector<Eigen::MatrixXd> samples;
    std::vector<std::string> sample_names;

    for (auto i : list_dir()) {
        if (contains_number(i)) {
            samples.push_back(load_csv(location + "/" + i));
            sample_names.push_back(i);
        }
    }

    content["samples"] = samples;
    content["sample_names"] = sample_names;

    return content;
}

显然,我收到了一个错误

Obviously, I am getting an error of

error: type/value mismatch at argument 2 in template parameter list for 'template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map'
         std::map<std::string, std::vector> get_data(void);

因为我在方法中没有 vector 的数据类型.如果我添加 stringEigen::MatrixXd 它们不能同时进行.我知道我可以使用 boost::any 但这将是一个需要编译的额外库(?),并且 C++ 不是动态类型的.

Because I don't have a datatype for vector in the method. If I add string or Eigen::MatrixXd they both cannot go hand-in-hand. I know that I could use boost::any but it would be an extra library which needs to be compiled (?), and C++ is not dynamically typed.

没有额外的库,我有什么办法可以做到这一点吗?

Is there any way I could do this by not have an extra library?

更新 1:

我认为我可以做的另一种方法是在构造函数中有一个私有变量 vector示例 &vectorsmaple_names 并创建两个相同向量类型的方法 get_samples()get_sample_names() 然后使用类似 this->samples = samplesthis->sample_name.这是唯一的其他方式吗?

Another way I think I could do is that in the constructor have a private variable of vector<Eigen::MatriXd> samples & vector<std::string> smaple_names and create two methods of same vector types get_samples() and get_sample_names() then use something like this->samples = samples and this->sample_name. Is this the only other way?

更新 2:

看起来 std::any 在 C++17 中 - http://en.cppreference.com/w/cpp/header/any.如果 anyone :p 感兴趣.

It looks like std::any is in C++17 - http://en.cppreference.com/w/cpp/header/any. If anyone :p interested.

更新 3:

如果有人在寻找答案,就在

If anyone is looking for the answer it's at

https://stackoverflow.com/a/44896365/3782963

推荐答案

我同意 std::any(或 boost::any)对于这个情况.std::variant(或 boost::variant)在这里会是更好的选择,因为您确切地知道要存储的 2 种类型.

I agree that std::any (or boost::any) is a bit overkill for this situation. std::variant (or boost::variant) would be a better choice here, because you know exactly what 2 types you want to store.

如果您不想使用任何其他库,例如 Boost 并且无法访问 C++17,您可以尝试在虚拟实现中模拟 std::variant(如果你不能/不想写一个完整的).类似的东西:

If you don't want to use any additional libraries like Boost and don't have access to C++17, you can try to emulate std::variant yourself in a dummy implementation (if you can't/don't want to write a full one). Something like:

struct data {
    // constructors...

    std::vector<Eigen::MatrixXd> samples;
    std::vector<std::string> names;
    int index = -1;
};

然后您可以将数据存储在 std::map<std::string, data>:

Then you can store your data in a std::map<std::string, data>:

std::map<std::string, data> content;
content.emplace(std::make_pair("samples", data{samples, 0}));
content.emplace(std::make_pair("sample_names", data{sample_names, 1}));

然后你只需要检查索引就知道你必须使用哪个std::vector.

Then you just need to check the index to know which std::vector you have to use.

这篇关于在 C++ 中返回字符串和不同向量数据类型的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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