I need to write a function that accepts a 2D string xtensor as input and returns a view on that xtensor that can be used to modify elements of the input xtensor from outside the function.

When I run this code the original myString xtensor is not modified. How can I modify it via the xtView?

main ()
{

   // Create a string xtensor

    xt::xtensor<std::string, 2> myString = xt::xtensor<std::string, 2>::from_shape({ 3, 4 });

    // Fill the xtensor with some initial values
    myString(0, 0) = "Here";
    myString(0, 1) = "everyone";
    myString(0, 2) = "likes";
    myString(0, 3) = "lollies";

    myString(1, 0) = "or";
    myString(1, 1) = "spaces";
    myString(1, 2) = "which";
    myString(1, 3) = "originate";

    myString(2, 0) = "real";
    myString(2, 1) = "loves";
    myString(2, 2) = "driving";
    myString(2, 3) = "!";

    // Print the original string tensor
    std::cout << "Original String Tensor:\n" << myString << std::endl;

    // get a view to the original string tensor
    auto xtView = xtGet_view (myString ); 

    // modify 2 elements via the view 
    xtView (0,0) = "MOD_ONE"
    xtView (1,0) = "MOD_TWO"

    std::cout << "Modified String Tensor:\n" << myString << std::endl;

return; 

}


xt::xtensor<std::string, 2> xtGet_view (xt::xtensor<std::string, 2> myString )

{

return xt::view (myString, xt::keep (xt::range(1,2)), xt::all()); 

}
0

There are 0 best solutions below