Converting a Tensor to List in C++


How to convert a torch::Tensor to a list in C++? I am looking for a C++ equivalent of the following python code:

tensor([[0., 6.],
        [1., 2.]])
[[0.0, 6.0], [1.0, 2.0]]


The list in C++ is different than the list in python.

I would assume you probably want to convert tensor to some C++ standard container (like vector etc.)?

I don’t think it is possible now using libtorch’s API,

but using Tensor.data_ptr<float>() will return the pointer float*. Then you can start from here to convert the internal data to the C++ container, for example, naive traversal like Convert tensor into an array or else.