相关疑难解决方法(0)

如何在不复制对象的情况下公开将C++对象返回给Python的函数?

另一个问题中,我学习了如何通过复制对象来公开将C++对象返回给Python的函数.必须执行副本似乎不是最佳的.如何在不复制的情况下返回对象?即如何直接访问self.thisptr.getPeaks(data)in 返回的峰值PyPeakDetection.getPeaks(在peak_detection_.pyx中定义)?

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);

    vector<Peak> test;
    test.push_back(peak1);
    test.push_back(peak2);

    return test;
}
Run Code Online (Sandbox Code Playgroud)

peak.hpp

#ifndef PEAK_H
#define …
Run Code Online (Sandbox Code Playgroud)

c++ python cython

6
推荐指数
1
解决办法
535
查看次数

标签 统计

c++ ×1

cython ×1

python ×1