2017-08-21 31 views
-2

是否有可能導入了std ::發現,在libcpp.algorithm我只能找到函數量非常有限那裏。現在我必須遍歷矢量並進行比較。有什麼辦法在Cython中導入std :: find find_if等?

+1

你是什麼意思_import_? '#include '是不夠的? – user0042

+0

你可以請你發佈你的代碼嗎? –

+1

@ user0042用Cython暴露了一些C++標準庫以類Python語言,它提供接口C++和Python的一種方式的。儘管如此,它並沒有特別完全暴露'',因此這個問題。 – DavidW

回答

3

你只需要按照用來包裹等功能的方案libcpp.algorithm

cdef extern from "<algorithm>" namespace "std": 
    Iter find_if[Iter, Func](Iter first, Iter last, Func pred) 

from libcpp.vector cimport vector 
from libcpp cimport bool 

cdef bool findtwo(int a): 
    if a==2: 
     return True 


def test(): 
    cdef vector[int] v = [1,2,3,4,2] 
    cdef vector[int].iterator found = find_if(v.begin(), v.end(), findtwo) 
    if found != v.end(): 
     print("Found") 

你會找到很大的限制是什麼,你可以通過作謂語的功能:它必須是一個cdef功能,這意味着沒有Python的倒閉,沒有可調用的Python對象,等等。另外要注意比你返回將被解讀爲true任何Python對象(即不是nullptr),所以一定要返回一個C++布爾,因爲我已經證明。

相關問題