如何将 xarray 转换为 std::vector?

Tom*_*Tom 7 stdvector xtensor

该文档非常清楚地说明了如何使 a 适应std::vector张量对象。 https://xtensor.readthedocs.io/en/latest/adaptor.html

std::vector<double> v = {1., 2., 3., 4., 5., 6. };
std::vector<std::size_t> shape = { 2, 3 };
auto a1 = xt::adapt(v, shape);
Run Code Online (Sandbox Code Playgroud)

但你怎么能反过来呢?

xt::xarray<double> a2 = { { 1., 2., 3.} };
std::vector<double> a2vector = ?;
Run Code Online (Sandbox Code Playgroud)

Tom*_*eus 5

您可以std::vector从迭代器构造一个。对于你的例子:

std::vector<double> w(a1.begin(), a1.end());
Run Code Online (Sandbox Code Playgroud)

完整的例子就变成了:

#include <vector>
#include <xtensor/xadapt.hpp>
#include <xtensor/xio.hpp>

int main()
{
    std::vector<double> v = {1., 2., 3., 4., 5., 6.};
    std::vector<std::size_t> shape = {2, 3};
    auto a1 = xt::adapt(v, shape);
    std::vector<double> w(a1.begin(), a1.end());
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

参考: