c++ - 如何将 xt::sum 表达式结果转换为整数

标签 c++ xtensor

我刚开始使用 xtensor,我已经陷入了一个基本问题。

我正在使用类似 xt::sum(xt::where(egoLaneLeftCount, 1, 0)) 的方法对列求和并获得单个整数值。我想将这个整数值保存到一个变量中,但出现以下编译错误

xt::xreducer<xt::xreducer_functors<std::plus<long long int>, xt::const_value<long long int>, std::plus<long long int> >, xt::xfunction<xt::detail::conditional_ternary, const xt::xarray_container<xt::uvector<bool, std::allocator<bool> >, (xt::layout_type)1, xt::svector<long unsigned int, 4, std::allocator<long unsigned int>, true>, xt::xtensor_expression_tag>&, xt::xscalar<int>, xt::xscalar<int> >, xt::svector<long unsigned int, 4, std::allocator<long unsigned int>, true>, xt::reducer_options<long long int, std::tuple<xt::evaluation_strategy::lazy_type> > >

任何人都知道如何将此数据类型的结果转换为 int 吗?

最佳答案

xt::sum 将返回“xtensor 类型”(xexpression)。它声明“仅在访问时或将表达式分配给容器时才计算值”。因此,要获得您想要的值,请尝试以下操作:

xt::xtensor<double, 1> egoLaneLeftCount = { 1, 1, 1, 0, 0 };
int x = xt::sum(xt::where(egoLaneLeftCount, 1, 0))[0];

关于c++ - 如何将 xt::sum 表达式结果转换为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58338761/

相关文章:

c++ 如何在不进行深度复制的情况下展平 xtensor 的 View ?

c++ - 在 C++ 中使用库 "xtensor-blas"时出错

c++ - 不完整类 c++ 的无效使用?

c++ - 将平铺 basemap 添加到 QGraphicsScene

c++ - Qt:使用uic生成ui_类,通过类名动态加载

c++ - 窗口不出现

python - xtensor 将 numpy 数组传递给具有 xt::xtensor 参数类型的函数

numpy - xtensor 相当于 numpy a[a>3] = 1

c++ - 我应该使用哪种参数类型来接受xexpressions?

c++ - 这会泄漏内存吗?