0

I'd like to get the index of the minimal element of a py::array_t variable in pybind11, but I encountered a problem when trying to combine std::distance and std::min_element.

let's say the py::array_t variable is named arr, the following code will get stuck when it's running:

std::distance(arr.begin(), std::min_element(arr.begin(), arr.end()))

initially I was thinking the py::array_t iterator is not compatible with std::distance function, but I did a test and the following code just works fine:

std::distance(arr.begin(), arr.end())

Can someone explain why the first one does not work? Thanks a lot.

2
  • 1
    Are you sure py:array_t is contiguous in memory? I never used numpy/pybind. But when I look at numpy/array_t it seems to be a more complicated datastructure than std::array is (and its operations suggest dynamic memory allocation is in play) Commented Jan 5 at 13:44
  • Why would it matter whether the memory is contiguous, as long as the iterators are implemented correctly and are of the correct categories? Commented Jan 5 at 13:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.