C++ std::any 如何检查 std::any 的类型是否为向量

发布于 2025-01-17 05:46:02 字数 1877 浏览 2 评论 0原文

#include <iostream>
#include <vector>
#include <string>
#include <any>
#include <map>
#include <functional>
#include <exception>
using namespace std;
using MapAny = std::map<string, any>;

int square(int x) {
    return x*x;
}

vector<int> parse(map<string, vector<MapAny>> mapping)
{
    vector<MapAny> func_square = mapping["square"];
    vector<int> res;
    for (const auto &mapany : func_square) {
        try {
            int x = any_cast<int>(mapany.at("x"));    
            res.push_back(square(x));
        }
        catch (exception e) {
            vector<int> xs = any_cast<vector<int>>(mapany.at("x"));
            for (int x : xs) res.push_back(square(x));
        }        
    }

    return res;
}

int main()
{
    map<string, vector<MapAny>> function_map_value, function_map_array;
    function_map_value = {
        {"square", { {{"x", 5}}, {{"x", 10}} }}
    };
    
    vector<MapAny> vec;    
    vec.push_back({{"x", vector<int>({5, 10}) }});
    function_map_array = {
        {"square", vec}
    };

    vector<int> res1 = parse(function_map_value);
    vector<int> res2 = parse(function_map_array);
    for (int i=0; i<res1.size(); i++) cout << res1[i] << " "; cout << "\n";
    for (int i=0; i<res2.size(); i++) cout << res2[i] << " "; cout << "\n";
    return 0;
}

我正在尝试制作一个可以接受任何类型的函数解析器,例如标量值和向量值,就像Python dict() 中一样。

但是,我不确定如何检查 std::any 对象是否具有 std::vector 类型。在上面的代码中,如果 any_cast 失败,它将抛出异常,并且我知道它是一个 std::vector。它很丑陋,并且依赖于抛出异常作为预期行为。

我怎样才能把上面的代码改成这样:

if (is_vector(mapany.at("x")) {
  // deal with vector
}
else {
  // deal with scalar
}
#include <iostream>
#include <vector>
#include <string>
#include <any>
#include <map>
#include <functional>
#include <exception>
using namespace std;
using MapAny = std::map<string, any>;

int square(int x) {
    return x*x;
}

vector<int> parse(map<string, vector<MapAny>> mapping)
{
    vector<MapAny> func_square = mapping["square"];
    vector<int> res;
    for (const auto &mapany : func_square) {
        try {
            int x = any_cast<int>(mapany.at("x"));    
            res.push_back(square(x));
        }
        catch (exception e) {
            vector<int> xs = any_cast<vector<int>>(mapany.at("x"));
            for (int x : xs) res.push_back(square(x));
        }        
    }

    return res;
}

int main()
{
    map<string, vector<MapAny>> function_map_value, function_map_array;
    function_map_value = {
        {"square", { {{"x", 5}}, {{"x", 10}} }}
    };
    
    vector<MapAny> vec;    
    vec.push_back({{"x", vector<int>({5, 10}) }});
    function_map_array = {
        {"square", vec}
    };

    vector<int> res1 = parse(function_map_value);
    vector<int> res2 = parse(function_map_array);
    for (int i=0; i<res1.size(); i++) cout << res1[i] << " "; cout << "\n";
    for (int i=0; i<res2.size(); i++) cout << res2[i] << " "; cout << "\n";
    return 0;
}

I'm trying to make a function parser that can accept any type, such as both scalar and vector values, like in Python dict().

However, I'm not sure how to check if a std::any object has type std::vector. In the code above, if any_cast fails, it will throw exception and I know it's a std::vector. It's ugly and rely on throwing exception as an expected behavior.

How can I change the above code into something like:

if (is_vector(mapany.at("x")) {
  // deal with vector
}
else {
  // deal with scalar
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的痛♀有谁懂 2025-01-24 05:46:02

因为我无法发表评论:

为什么你不使用以下方法比较类型:

if (mapany.at("x").type() == typeid(std::vector<int>))  
{
}

Since I cannot leave a comment:

Why you don't compare types using:

if (mapany.at("x").type() == typeid(std::vector<int>))  
{
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文