代码之家  ›  专栏  ›  技术社区  ›  The Unfun Cat

如何使用c++std::merge合并cython中的两个排序向量?

  •  1
  • The Unfun Cat  · 技术社区  · 6 年前

    1 回复  |  直到 6 年前
        1
  •  2
  •   ead    6 年前

    不是所有的c++方法都包装在Cython的cpplib中,而是使用 already wrapped methods 作为蓝图,很容易包装缺少的功能-无需重新实现算法(无论多么简单)

    例如:

    %%cython  --cplus 
    from libcpp.vector cimport vector
    
    # wrap it yourself!
    cdef extern from "<algorithm>" namespace "std" nogil:
       OutputIter merge[InputIter1, InputIter2, OutputIter] (InputIter1 first1, InputIter1 last1,
                            InputIter2 first2, InputIter2 last2,
                            OutputIter result)
    
    # for ilustration purposes:
    cdef vector[int] v1=[1,3,5]
    cdef vector[int] v2=[2,4,6]
    cdef vector[int] out = vector[int](6)
    
    merge(v1.begin(), v1.end(), v2.begin(), v2.end(), out.begin())
    
    print(out)
    

    [1,2,3,4,5,6]