【发布时间】:2017-03-21 15:48:27
【问题描述】:
使用PyArray_SimpleNewFromData,很容易将std::vector公开为一个numpy数组。我现在正试图做相反的事情:将一个 numpy 数组公开为一个 c++ 向量。
可以公开为 C 数组:
// get the size
npy_intp s = PyArray_SIZE(numpy_array);
// get the pointer to the array
bool* c_array = (bool*) PyArray_GETPTR1( numpy_array, 0 );
// Do something
for(unsigned int i=0; i<s; i++)
c_array[i] = ... ;
现在用 c++ 向量代替 c 数组怎么样?
编辑:我不想复制数据,否则答案是微不足道的。
【问题讨论】:
-
您可以简单地使用数组作为初始化器来构造一个新向量...
-
@J.H.Bonarius,我编辑了问题以指定我不希望复制数据。只“暴露”它。例如,我可能想修改数组的一个元素。