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

How to convert xt::sum expression result to an integer

本文关键字:结果 转换 整数 表达式 sum xt      更新时间:2023-10-16

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

我正在使用类似xt::sum(xt::where(egoLaneLeftCount, 1, 0))的东西对一列求和并得到一个整数值。我想将此整数值保存到变量中,但得到以下compilation error

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 type" (xexpression(。它指出"仅在访问或将表达式分配给容器时计算值"。因此,要获得所需的值,请尝试以下操作:

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