在Python中传递一个c++ std:::向量到numpy数组

时间:2021-01-20 21:21:41

I am trying a pass a vector of doubles that I generate in my C++ code to a python numpy array. I am looking to do some downstream processing in Python and want to use some python facilities, once I populate the numpy array. One of the biggest things I want to do is to be able to plot things, and C++ is a bit clumsy when it comes to that. Also I want to be able to leverage Python's statistical power.

我尝试将在c++代码中生成的一个双精度向量传递给python numpy数组。我希望在Python中进行一些下游处理,并希望在填充numpy数组之后使用一些Python工具。我想做的最重要的事情之一是能够绘制东西,而c++在这方面有点笨拙。我还希望能够利用Python的统计能力。

Though I am not very clear as to how to do it. I spent a lot of time going through the Python C API documentation. I came across a function PyArray_SimpleNewFromData that apparently can do the trick. I still am very unclear as far as the overall set up of the code is concerned. I am building certain very simple test cases to help me understand this process. I generated the following code as a standlone Empty project in Visual Studio express 2012. I call this file Project1

虽然我不太清楚该怎么做。我花了很多时间研究Python C API文档。我遇到了一个函数PyArray_SimpleNewFromData,它显然可以做到这一点。就代码的总体设置而言,我仍然非常不清楚。我正在构建一些非常简单的测试用例来帮助我理解这个过程。我在Visual Studio express 2012中生成了如下代码作为一个独立的空项目。我把这个文件命名为Project1

#include <Python.h>
#include "C:/Python27/Lib/site-packages/numpy/core/include/numpy/arrayobject.h"

PyObject * testCreatArray()
{
    float fArray[5] = {0,1,2,3,4};
    npy_intp m = 5;
    PyObject * c = PyArray_SimpleNewFromData(1,&m,PyArray_FLOAT,fArray);
    return c; 
}

My goal is to be able to read the PyObject in Python. I am stuck because I don't know how to reference this module in Python. In particular how do I import this Project from Python, I tried to do a import Project1, from the project path in python, but failed. Once I understand this base case, my goal is to figure out a way to pass the vector container that I compute in my main function to Python. I am not sure how to do that either.

我的目标是能够在Python中读取PyObject。我被困住了,因为我不知道如何在Python中引用这个模块。特别是如何从Python中导入这个项目,我尝试从Python中的项目路径中导入Project1,但失败了。一旦我理解了这个基本情况,我的目标就是找出一种将我在主函数中计算的向量容器传递给Python的方法。我也不知道该怎么做。

Any experts who can help me with this, or maybe post a simple well contained example of some code that reads in and populates a numpy array from a simple c++ vector, I will be grateful. Many thanks in advance.

任何可以帮助我做这个的专家,或者发布一个简单的包含良好的代码示例,从一个简单的c++向量读取并填充一个numpy数组,我将非常感激。提前感谢。

4 个解决方案

#1


3  

Since there is no answer to this that is actually helpful for people that might be looking for this sort of thing I figured I'd put an easy solution.

因为这个问题没有答案,这对可能寻找这种东西的人很有帮助,我想我会放一个简单的解决方案。

First you will need to create a python extension module in C++, this is easy enough to do and is all in the python c-api documentation so i'm not going to go into that.

首先,您需要在c++中创建一个python扩展模块,这很容易做到,并且都在python C -api文档中,所以我不打算详细介绍。

Now to convert a c++ std::vector to a numpy array is extremely simple. You first need to import the numpy array header

现在要将c++ std::vector转换为numpy数组非常简单。首先需要导入numpy数组头

#include <numpy/arrayobject.h>

and in your intialising function you need to import_array()

在初始化函数中,需要导入_array()

PyModINIT_FUNC
inittestFunction(void){
   (void) Py_InitModule("testFunction". testFunctionMethods);
   import_array();
}

now you can use the numpy array functions that are provided. The one that you will want for this is as the OP said a few years back PyArray_SimpleNewFromData, it's stupidly simple to use. All you need is an array of type npy_intp, this is the shape of the array to be created. make sure it is the same as your vector using testVector.size(), (and for multiple dimensions do testVector[0].size(), testVector[0][0].size() ect. vectors are guaranteed to be continuous in c++11 unless it's a bool).

现在可以使用提供的numpy数组函数。正如OP几年前的PyArray_SimpleNewFromData所言,您需要的就是这样,使用它非常简单。您只需要一个npy_intp类型的数组,这是要创建的数组的形状。确保它与使用testVector.size()的向量相同,(对于多个维,testVector[0].size(), testVector[0].size()等)。向量在c++11中保证是连续的,除非它是一个bool。

//create testVector with data initialised to 0
std::vector<std::vector<uint16_t>> testVector;
testVector.resize(width, std::vector<uint16_t>(height, 0);
//create shape for numpy array
npy_intp dims[2] = {width, height}
//convert testVector to a numpy array
PyArrayObject* numpyArray = (PyArrayObject*)PyArray_SimpleNewFromData(2, dims, NPY_UINT16, (uint16_t*)testVector.data());

To go through the paramaters. First you need to cast it to a PyArrayObject, otherwise it will be a PyObject and when returned to python won't be a numpy array. The 2, is the number of dimensions in the array. dims, is the shape of the array. This has to be of type npy_intp NPY_UINT16 is the data type that the array will be in python. you then use testVector.data() to get the data of the array, cast this to either void* or a pointer of the same data type as your vector.

要穿过这些护理员。首先需要将它转换为PyArrayObject,否则它将是PyObject,返回到python时不会是numpy数组。2是数组中的维数。dims,是数组的形状。这必须是npy_intp NPY_UINT16类型,它是数组在python中的数据类型。然后使用testVector.data()获取数组的数据,将其转换为void*或与向量具有相同数据类型的指针。

Hope this helps anyone else who may need this.

希望这能帮助其他需要帮助的人。

(Also if you don't need pure speed I would advise avoiding using the C-API, it causes quite a few problems and cython or swig are still probably your best choices. There is also c types which can be quite helpful.

(如果您不需要纯粹的速度,我建议您避免使用C-API,它会导致很多问题,cython或swig可能仍然是您的最佳选择。也有一些c类型是非常有用的。

#2


3  

I'm not a cpp-hero ,but wanted to provide my solution with two template functions for 1D and 2D vectors. This is a one liner for usage l8ter and by templating 1D and 2D vectors, the compiler can take the correct version for your vectors shape. Throws a string in case of unregular shape in the case of 2D. The routine copies the data here, but one can easily modify it to take the adress of the first element of the input vector in order to make it just a "representation".

我不是cpphero,但是我想为我的解决方案提供两个用于1D和2D向量的模板函数。这是使用l8ter的一行代码,通过模板化1D和2D向量,编译器可以为向量的形状选择正确的版本。在二维的情况下抛出不规则形状的字符串。例程在这里复制数据,但是可以很容易地对其进行修改,使其成为输入向量的第一个元素的地址,从而使其成为一个“表示”。

Usage looks like this:

使用看起来像这样:

// Random data
vector<float> some_vector_1D(3,1.f); // 3 entries set to 1
vector< vector<float> > some_vector_2D(3,vector<float>(3,1.f)); // 3 subvectors with 1

// Convert vectors to numpy arrays
PyObject* np_vec_1D = (PyObject*) vector_to_nparray(some_vector_1D);
PyObject* np_vec_2D = (PyObject*) vector_to_nparray(some_vector_2D);

You may also change the type of the numpy array by the optional arguments. The template functions are:

您还可以通过可选参数更改numpy数组的类型。模板函数是:

/** Convert a c++ 2D vector into a numpy array
 *
 * @param const vector< vector<T> >& vec : 2D vector data
 * @return PyArrayObject* array : converted numpy array
 *
 * Transforms an arbitrary 2D C++ vector into a numpy array. Throws in case of
 * unregular shape. The array may contain empty columns or something else, as
 * long as it's shape is square.
 *
 * Warning this routine makes a copy of the memory!
 */
template<typename T>
static PyArrayObject* vector_to_nparray(const vector< vector<T> >& vec, int type_num = PyArray_FLOAT){

   // rows not empty
   if( !vec.empty() ){

      // column not empty
      if( !vec[0].empty() ){

        size_t nRows = vec.size();
        size_t nCols = vec[0].size();
        npy_intp dims[2] = {nRows, nCols};
        PyArrayObject* vec_array = (PyArrayObject *) PyArray_SimpleNew(2, dims, type_num);

        T *vec_array_pointer = (T*) PyArray_DATA(vec_array);

        // copy vector line by line ... maybe could be done at one
        for (size_t iRow=0; iRow < vec.size(); ++iRow){

          if( vec[iRow].size() != nCols){
             Py_DECREF(vec_array); // delete
             throw(string("Can not convert vector<vector<T>> to np.array, since c++ matrix shape is not uniform."));
          }

          copy(vec[iRow].begin(),vec[iRow].end(),vec_array_pointer+iRow*nCols);
        }

        return vec_array;

     // Empty columns
     } else {
        npy_intp dims[2] = {vec.size(), 0};
        return (PyArrayObject*) PyArray_ZEROS(2, dims, PyArray_FLOAT, 0);
     }


   // no data at all
   } else {
      npy_intp dims[2] = {0, 0};
      return (PyArrayObject*) PyArray_ZEROS(2, dims, PyArray_FLOAT, 0);
   }

}


/** Convert a c++ vector into a numpy array
 *
 * @param const vector<T>& vec : 1D vector data
 * @return PyArrayObject* array : converted numpy array
 *
 * Transforms an arbitrary C++ vector into a numpy array. Throws in case of
 * unregular shape. The array may contain empty columns or something else, as
 * long as it's shape is square.
 *
 * Warning this routine makes a copy of the memory!
 */
template<typename T>
static PyArrayObject* vector_to_nparray(const vector<T>& vec, int type_num = PyArray_FLOAT){

   // rows not empty
   if( !vec.empty() ){

       size_t nRows = vec.size();
       npy_intp dims[1] = {nRows};

       PyArrayObject* vec_array = (PyArrayObject *) PyArray_SimpleNew(1, dims, type_num);
       T *vec_array_pointer = (T*) PyArray_DATA(vec_array);

       copy(vec.begin(),vec.end(),vec_array_pointer);
       return vec_array;

   // no data at all
   } else {
      npy_intp dims[1] = {0};
      return (PyArrayObject*) PyArray_ZEROS(1, dims, PyArray_FLOAT, 0);
   }

}

#3


1  

I came across your post when trying to do something very similar. I was able to cobble together a solution, the entirety of which is on my Github. It makes two C++ vectors, converts them to Python tuples, passes them to Python, converts them to NumPy arrays, then plots them using Matplotlib.

我在尝试做类似的事情时偶然发现了你的帖子。我能够拼凑出一种解决方案,整个方案都在我的Github上。它生成两个c++向量,将它们转换为Python元组,将它们传递给Python,将它们转换为NumPy数组,然后使用Matplotlib绘制它们。

Much of this code is from the Python Documentation.

这些代码大部分来自Python文档。

Here are some of the important bits from the .cpp file :

以下是。cpp文件中的一些重要部分:

 //Make some vectors containing the data
 static const double xarr[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14};
 std::vector<double> xvec (xarr, xarr + sizeof(xarr) / sizeof(xarr[0]) );
 static const double yarr[] = {0,0,1,1,0,0,2,2,0,0,1,1,0,0};
 std::vector<double> yvec (yarr, yarr + sizeof(yarr) / sizeof(yarr[0]) );

 //Transfer the C++ vector to a python tuple
 pXVec = PyTuple_New(xvec.size()); 
 for (i = 0; i < xvec.size(); ++i) {
      pValue = PyFloat_FromDouble(xvec[i]);
      if (!pValue) {
           Py_DECREF(pXVec);
           Py_DECREF(pModule);
           fprintf(stderr, "Cannot convert array value\n");
           return 1;
      }
      PyTuple_SetItem(pXVec, i, pValue);
 }

 //Transfer the other C++ vector to a python tuple
 pYVec = PyTuple_New(yvec.size()); 
 for (i = 0; i < yvec.size(); ++i) {
      pValue = PyFloat_FromDouble(yvec[i]);
      if (!pValue) {
           Py_DECREF(pYVec);
           Py_DECREF(pModule);
           fprintf(stderr, "Cannot convert array value\n");
           return 1;
      }
      PyTuple_SetItem(pYVec, i, pValue); //
 }

 //Set the argument tuple to contain the two input tuples
 PyTuple_SetItem(pArgTuple, 0, pXVec);
 PyTuple_SetItem(pArgTuple, 1, pYVec);

 //Call the python function
 pValue = PyObject_CallObject(pFunc, pArgTuple);

And the Python code:

和Python代码:

def plotStdVectors(x, y):
    import numpy as np
    import matplotlib.pyplot as plt
    print "Printing from Python in plotStdVectors()"
    print x
    print y
    x = np.fromiter(x, dtype = np.float)
    y = np.fromiter(y, dtype = np.float)
    print x
    print y
    plt.plot(x, y)
    plt.show()
    return 0

Which results in the plot that I can't post here due to my reputation, but is posted on my blog post here.

因为我的名声,我不能在这里发布这个情节,但是在我的博客上。

#4


0  

  _import_array(); //this is required for numpy to create an array correctly

Note: In Numpy's extension guide they use import_array() to accomplish the same goal that I used _import_array() for. When I tried using import_array(), on a mac I got an error. So you may need to try both commands and see which one works.

注意:在Numpy的扩展指南中,它们使用import_array()来实现我使用_import_array()实现的目标。当我尝试使用import_array()时,在mac上我得到了一个错误。因此,您可能需要同时尝试这两个命令,并查看哪个可以工作。

By the way you can use C++ std::vector in the call to PyArray_SimpleNewFromData. If your std::vector is my_vector, replace fArraywith &my_vector[0]. &my_vector[0] allows you to access the pointer that stores the data in my_vector.

顺便说一下,您可以在调用PyArray_SimpleNewFromData时使用c++ std:::vector。如果你的std::vector是my_vector,用&my_vector[0]替换fArraywith。my_vector[0]允许您访问在my_vector中存储数据的指针。

#1


3  

Since there is no answer to this that is actually helpful for people that might be looking for this sort of thing I figured I'd put an easy solution.

因为这个问题没有答案,这对可能寻找这种东西的人很有帮助,我想我会放一个简单的解决方案。

First you will need to create a python extension module in C++, this is easy enough to do and is all in the python c-api documentation so i'm not going to go into that.

首先,您需要在c++中创建一个python扩展模块,这很容易做到,并且都在python C -api文档中,所以我不打算详细介绍。

Now to convert a c++ std::vector to a numpy array is extremely simple. You first need to import the numpy array header

现在要将c++ std::vector转换为numpy数组非常简单。首先需要导入numpy数组头

#include <numpy/arrayobject.h>

and in your intialising function you need to import_array()

在初始化函数中,需要导入_array()

PyModINIT_FUNC
inittestFunction(void){
   (void) Py_InitModule("testFunction". testFunctionMethods);
   import_array();
}

now you can use the numpy array functions that are provided. The one that you will want for this is as the OP said a few years back PyArray_SimpleNewFromData, it's stupidly simple to use. All you need is an array of type npy_intp, this is the shape of the array to be created. make sure it is the same as your vector using testVector.size(), (and for multiple dimensions do testVector[0].size(), testVector[0][0].size() ect. vectors are guaranteed to be continuous in c++11 unless it's a bool).

现在可以使用提供的numpy数组函数。正如OP几年前的PyArray_SimpleNewFromData所言,您需要的就是这样,使用它非常简单。您只需要一个npy_intp类型的数组,这是要创建的数组的形状。确保它与使用testVector.size()的向量相同,(对于多个维,testVector[0].size(), testVector[0].size()等)。向量在c++11中保证是连续的,除非它是一个bool。

//create testVector with data initialised to 0
std::vector<std::vector<uint16_t>> testVector;
testVector.resize(width, std::vector<uint16_t>(height, 0);
//create shape for numpy array
npy_intp dims[2] = {width, height}
//convert testVector to a numpy array
PyArrayObject* numpyArray = (PyArrayObject*)PyArray_SimpleNewFromData(2, dims, NPY_UINT16, (uint16_t*)testVector.data());

To go through the paramaters. First you need to cast it to a PyArrayObject, otherwise it will be a PyObject and when returned to python won't be a numpy array. The 2, is the number of dimensions in the array. dims, is the shape of the array. This has to be of type npy_intp NPY_UINT16 is the data type that the array will be in python. you then use testVector.data() to get the data of the array, cast this to either void* or a pointer of the same data type as your vector.

要穿过这些护理员。首先需要将它转换为PyArrayObject,否则它将是PyObject,返回到python时不会是numpy数组。2是数组中的维数。dims,是数组的形状。这必须是npy_intp NPY_UINT16类型,它是数组在python中的数据类型。然后使用testVector.data()获取数组的数据,将其转换为void*或与向量具有相同数据类型的指针。

Hope this helps anyone else who may need this.

希望这能帮助其他需要帮助的人。

(Also if you don't need pure speed I would advise avoiding using the C-API, it causes quite a few problems and cython or swig are still probably your best choices. There is also c types which can be quite helpful.

(如果您不需要纯粹的速度,我建议您避免使用C-API,它会导致很多问题,cython或swig可能仍然是您的最佳选择。也有一些c类型是非常有用的。

#2


3  

I'm not a cpp-hero ,but wanted to provide my solution with two template functions for 1D and 2D vectors. This is a one liner for usage l8ter and by templating 1D and 2D vectors, the compiler can take the correct version for your vectors shape. Throws a string in case of unregular shape in the case of 2D. The routine copies the data here, but one can easily modify it to take the adress of the first element of the input vector in order to make it just a "representation".

我不是cpphero,但是我想为我的解决方案提供两个用于1D和2D向量的模板函数。这是使用l8ter的一行代码,通过模板化1D和2D向量,编译器可以为向量的形状选择正确的版本。在二维的情况下抛出不规则形状的字符串。例程在这里复制数据,但是可以很容易地对其进行修改,使其成为输入向量的第一个元素的地址,从而使其成为一个“表示”。

Usage looks like this:

使用看起来像这样:

// Random data
vector<float> some_vector_1D(3,1.f); // 3 entries set to 1
vector< vector<float> > some_vector_2D(3,vector<float>(3,1.f)); // 3 subvectors with 1

// Convert vectors to numpy arrays
PyObject* np_vec_1D = (PyObject*) vector_to_nparray(some_vector_1D);
PyObject* np_vec_2D = (PyObject*) vector_to_nparray(some_vector_2D);

You may also change the type of the numpy array by the optional arguments. The template functions are:

您还可以通过可选参数更改numpy数组的类型。模板函数是:

/** Convert a c++ 2D vector into a numpy array
 *
 * @param const vector< vector<T> >& vec : 2D vector data
 * @return PyArrayObject* array : converted numpy array
 *
 * Transforms an arbitrary 2D C++ vector into a numpy array. Throws in case of
 * unregular shape. The array may contain empty columns or something else, as
 * long as it's shape is square.
 *
 * Warning this routine makes a copy of the memory!
 */
template<typename T>
static PyArrayObject* vector_to_nparray(const vector< vector<T> >& vec, int type_num = PyArray_FLOAT){

   // rows not empty
   if( !vec.empty() ){

      // column not empty
      if( !vec[0].empty() ){

        size_t nRows = vec.size();
        size_t nCols = vec[0].size();
        npy_intp dims[2] = {nRows, nCols};
        PyArrayObject* vec_array = (PyArrayObject *) PyArray_SimpleNew(2, dims, type_num);

        T *vec_array_pointer = (T*) PyArray_DATA(vec_array);

        // copy vector line by line ... maybe could be done at one
        for (size_t iRow=0; iRow < vec.size(); ++iRow){

          if( vec[iRow].size() != nCols){
             Py_DECREF(vec_array); // delete
             throw(string("Can not convert vector<vector<T>> to np.array, since c++ matrix shape is not uniform."));
          }

          copy(vec[iRow].begin(),vec[iRow].end(),vec_array_pointer+iRow*nCols);
        }

        return vec_array;

     // Empty columns
     } else {
        npy_intp dims[2] = {vec.size(), 0};
        return (PyArrayObject*) PyArray_ZEROS(2, dims, PyArray_FLOAT, 0);
     }


   // no data at all
   } else {
      npy_intp dims[2] = {0, 0};
      return (PyArrayObject*) PyArray_ZEROS(2, dims, PyArray_FLOAT, 0);
   }

}


/** Convert a c++ vector into a numpy array
 *
 * @param const vector<T>& vec : 1D vector data
 * @return PyArrayObject* array : converted numpy array
 *
 * Transforms an arbitrary C++ vector into a numpy array. Throws in case of
 * unregular shape. The array may contain empty columns or something else, as
 * long as it's shape is square.
 *
 * Warning this routine makes a copy of the memory!
 */
template<typename T>
static PyArrayObject* vector_to_nparray(const vector<T>& vec, int type_num = PyArray_FLOAT){

   // rows not empty
   if( !vec.empty() ){

       size_t nRows = vec.size();
       npy_intp dims[1] = {nRows};

       PyArrayObject* vec_array = (PyArrayObject *) PyArray_SimpleNew(1, dims, type_num);
       T *vec_array_pointer = (T*) PyArray_DATA(vec_array);

       copy(vec.begin(),vec.end(),vec_array_pointer);
       return vec_array;

   // no data at all
   } else {
      npy_intp dims[1] = {0};
      return (PyArrayObject*) PyArray_ZEROS(1, dims, PyArray_FLOAT, 0);
   }

}

#3


1  

I came across your post when trying to do something very similar. I was able to cobble together a solution, the entirety of which is on my Github. It makes two C++ vectors, converts them to Python tuples, passes them to Python, converts them to NumPy arrays, then plots them using Matplotlib.

我在尝试做类似的事情时偶然发现了你的帖子。我能够拼凑出一种解决方案,整个方案都在我的Github上。它生成两个c++向量,将它们转换为Python元组,将它们传递给Python,将它们转换为NumPy数组,然后使用Matplotlib绘制它们。

Much of this code is from the Python Documentation.

这些代码大部分来自Python文档。

Here are some of the important bits from the .cpp file :

以下是。cpp文件中的一些重要部分:

 //Make some vectors containing the data
 static const double xarr[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14};
 std::vector<double> xvec (xarr, xarr + sizeof(xarr) / sizeof(xarr[0]) );
 static const double yarr[] = {0,0,1,1,0,0,2,2,0,0,1,1,0,0};
 std::vector<double> yvec (yarr, yarr + sizeof(yarr) / sizeof(yarr[0]) );

 //Transfer the C++ vector to a python tuple
 pXVec = PyTuple_New(xvec.size()); 
 for (i = 0; i < xvec.size(); ++i) {
      pValue = PyFloat_FromDouble(xvec[i]);
      if (!pValue) {
           Py_DECREF(pXVec);
           Py_DECREF(pModule);
           fprintf(stderr, "Cannot convert array value\n");
           return 1;
      }
      PyTuple_SetItem(pXVec, i, pValue);
 }

 //Transfer the other C++ vector to a python tuple
 pYVec = PyTuple_New(yvec.size()); 
 for (i = 0; i < yvec.size(); ++i) {
      pValue = PyFloat_FromDouble(yvec[i]);
      if (!pValue) {
           Py_DECREF(pYVec);
           Py_DECREF(pModule);
           fprintf(stderr, "Cannot convert array value\n");
           return 1;
      }
      PyTuple_SetItem(pYVec, i, pValue); //
 }

 //Set the argument tuple to contain the two input tuples
 PyTuple_SetItem(pArgTuple, 0, pXVec);
 PyTuple_SetItem(pArgTuple, 1, pYVec);

 //Call the python function
 pValue = PyObject_CallObject(pFunc, pArgTuple);

And the Python code:

和Python代码:

def plotStdVectors(x, y):
    import numpy as np
    import matplotlib.pyplot as plt
    print "Printing from Python in plotStdVectors()"
    print x
    print y
    x = np.fromiter(x, dtype = np.float)
    y = np.fromiter(y, dtype = np.float)
    print x
    print y
    plt.plot(x, y)
    plt.show()
    return 0

Which results in the plot that I can't post here due to my reputation, but is posted on my blog post here.

因为我的名声,我不能在这里发布这个情节,但是在我的博客上。

#4


0  

  _import_array(); //this is required for numpy to create an array correctly

Note: In Numpy's extension guide they use import_array() to accomplish the same goal that I used _import_array() for. When I tried using import_array(), on a mac I got an error. So you may need to try both commands and see which one works.

注意:在Numpy的扩展指南中,它们使用import_array()来实现我使用_import_array()实现的目标。当我尝试使用import_array()时,在mac上我得到了一个错误。因此,您可能需要同时尝试这两个命令,并查看哪个可以工作。

By the way you can use C++ std::vector in the call to PyArray_SimpleNewFromData. If your std::vector is my_vector, replace fArraywith &my_vector[0]. &my_vector[0] allows you to access the pointer that stores the data in my_vector.

顺便说一下,您可以在调用PyArray_SimpleNewFromData时使用c++ std:::vector。如果你的std::vector是my_vector,用&my_vector[0]替换fArraywith。my_vector[0]允许您访问在my_vector中存储数据的指针。