相关疑难解决方法(0)

如何使用Cython公开将C++对象返回给Python的函数?

我正在使用Cython构建C++ < - > Python绑定,我找不到如何从Python方法返回C++对象.

更具体地说,在编译时peak_detection_.pyx,如下所示,我得到了

peak_detection_.pyx:35:36: Cannot convert 'vector[Peak]' to Python object
Run Code Online (Sandbox Code Playgroud)

为最后一行

def getPeaks(self,data):
    return self.thisptr.getPeaks(data)
Run Code Online (Sandbox Code Playgroud)

我理解错误,但我不介意一些关于如何修复它的帮助/指针.

peak_detection.hpp

#ifndef PEAKDETECTION_H
#define PEAKDETECTION_H

#include <string>
#include <map>
#include <vector>

#include "peak.hpp"


class PeakDetection
{
    public:
        PeakDetection(std::map<std::string, std::string> config);
        std::vector<Peak> getPeaks(std::vector<float> &data);

    private:
        float _threshold;               
};

#endif
Run Code Online (Sandbox Code Playgroud)

peak_detection.cpp

#include <iostream>
#include <string>

#include "peak.hpp"
#include "peak_detection.hpp"


using namespace std;


PeakDetection::PeakDetection(map<string, string> config)
{   
    _threshold = stof(config["_threshold"]);
}

vector<Peak> PeakDetection::getPeaks(vector<float> &data){

    Peak peak1 = Peak(10,1);
    Peak peak2 = Peak(20,2); …
Run Code Online (Sandbox Code Playgroud)

c++ python cython

5
推荐指数
1
解决办法
611
查看次数

Cython:C++ 在字典中使用向量?

我正在使用以下代码尝试使用 C++ 向量:

from libcpp.vector cimport vector                                                                                                                                         

cdef struct StartEnd:
    long start, end 

cdef vector[StartEnd] vect
print(type(vect))
cdef int i
cdef StartEnd j
k = {}
k['hi'] = vect
for i in range(10):
    j.start = i 
    j.end = i + 2 
    k['hi'].push_back(j)
for i in range(10):
    print(k['hi'][i])
Run Code Online (Sandbox Code Playgroud)

这里的确切功能并不重要,这只是一个虚拟程序。问题是运行它会产生错误:AttributeError: 'list' object has no attribute 'push_back'如果没有字典,这会起作用,但我认为字典对于我的用例是必要的。有没有办法使这项工作?

我不想来回复制向量,因为这些向量将达到数千万个条目的长度。也许我可以存储指向向量的指针?

c++ python cython

4
推荐指数
1
解决办法
1242
查看次数

标签 统计

c++ ×2

cython ×2

python ×2