2015-09-21 2 views
0

У меня возникли проблемы с использованием libcpp.algorithm.sort (std::sort) на моем libcpp.vector. Это мой короткий код ниже:Cython: std :: sort on C++ vector

from libcpp.algorithm cimport sort as stdsort 
from libcpp.vector cimport vector 

cdef sort_something(mylist): 
    myvec = vector[int]() 
    for num in mylist: myvec.push_back(num) 
    stdsort(myvec.begin(), myvec.end()) 

Это использует стандартный синтаксис для использования std::sort на C++ vector а. Я получаю сердитые сообщения компилятора. Для справки, это мой setup.py файл:

from distutils.core import setup 
from Cython.Build import cythonize 

setup(
    ext_modules=cythonize("*.pyx", language="c++") 
) 

Это выход компилятора. (Внимание: Это долго, и я не могу понять.)

python setup.py build_ext --inplace 
running build_ext 
building 'sample' extension 
cc -fno-strict-aliasing -fno-common -dynamic -arch x86_64 -arch i386 -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch x86_64 -arch i386 -pipe -I/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/include -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c sample.cpp -o build/temp.macosx-10.10-intel-2.7/sample.o 
In file included from sample.cpp:257: 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:702:71: error: invalid operands to binary expression ('const _object' and 'const _object') 
    bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;} 
                    ~~~^~~~ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3862:17: note: in instantiation of member function 'std::__1::__less<_object, _object>::operator()' requested here 
      if (__comp(*--__last, *__first)) 
       ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:4048:5: note: in instantiation of function template specialization 'std::__1::__sort<std::__1::__less<_object, _object> &, _object *>' requested here 
    __sort<_Comp_ref>(__first, __last, __comp); 
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:4057:12: note: in instantiation of function template specialization 'std::__1::sort<_object *, std::__1::__less<_object, _object> >' requested here 
    _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 
     ^
sample.cpp:855:8: note: in instantiation of function template specialization 'std::__1::sort<_object *>' requested here 
    std::sort<PyObject *>(__pyx_t_2, __pyx_t_5); 
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:424:1: note: candidate template ignored: could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'const _object' 
operator< (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:592:1: note: candidate template ignored: could not match 'reverse_iterator<type-parameter-0-0>' against 'const _object' 
operator<(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:996:1: note: candidate template ignored: could not match 'move_iterator<type-parameter-0-0>' against 'const _object' 
operator<(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:1312:1: note: candidate template ignored: could not match '__wrap_iter<type-parameter-0-0>' against 'const _object' 
operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2917:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const _object' 
operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2975:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const _object' 
operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2984:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const _object' 
operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4774:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'const _object' 
operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4839:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'const _object' 
operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4847:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'const _object' 
operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT 
^ 
In file included from sample.cpp:257: 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3649:20: error: no matching function for call to '__sort3' 
    unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c); 
        ^~~~~~~~~~~~~~~~~ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3869:20: note: in instantiation of function template specialization 'std::__1::__sort4<std::__1::__less<_object, _object> &, _object *>' requested here 
      _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp); 
       ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:4048:5: note: in instantiation of function template specialization 'std::__1::__sort<std::__1::__less<_object, _object> &, _object *>' requested here 
    __sort<_Comp_ref>(__first, __last, __comp); 
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:4057:12: note: in instantiation of function template specialization 'std::__1::sort<_object *, std::__1::__less<_object, _object> >' requested here 
    _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 
     ^
sample.cpp:855:8: note: in instantiation of function template specialization 'std::__1::sort<_object *>' requested here 
    std::sort<PyObject *>(__pyx_t_2, __pyx_t_5); 
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3609:1: note: candidate template ignored: substitution failure [with _Compare = std::__1::__less<_object, _object> &, _ForwardIterator = _object *] 
__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3675:20: error: no matching function for call to '__sort4' 
    unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c); 
        ^~~~~~~~~~~~~~~~~ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3872:20: note: in instantiation of function template specialization 'std::__1::__sort5<std::__1::__less<_object, _object> &, _object *>' requested here 
      _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp); 
       ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:4048:5: note: in instantiation of function template specialization 'std::__1::__sort<std::__1::__less<_object, _object> &, _object *>' requested here 
    __sort<_Comp_ref>(__first, __last, __comp); 
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:4057:12: note: in instantiation of function template specialization 'std::__1::sort<_object *, std::__1::__less<_object, _object> >' requested here 
    _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 
     ^
sample.cpp:855:8: note: in instantiation of function template specialization 'std::__1::sort<_object *>' requested here 
    std::sort<PyObject *>(__pyx_t_2, __pyx_t_5); 
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3646:1: note: candidate template ignored: substitution failure [with _Compare = std::__1::__less<_object, _object> &, _ForwardIterator = _object *] 
__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3, 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3740:5: error: no matching function for call to '__sort3' 
    __sort3<_Compare>(__first, __first+1, __j, __comp); 
    ^~~~~~~~~~~~~~~~~ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3877:20: note: in instantiation of function template specialization 'std::__1::__insertion_sort_3<std::__1::__less<_object, _object> &, _object *>' requested 
     here 
      _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp); 
       ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:4048:5: note: in instantiation of function template specialization 'std::__1::__sort<std::__1::__less<_object, _object> &, _object *>' requested here 
    __sort<_Comp_ref>(__first, __last, __comp); 
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:4057:12: note: in instantiation of function template specialization 'std::__1::sort<_object *, std::__1::__less<_object, _object> >' requested here 
    _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 
     ^
sample.cpp:855:8: note: in instantiation of function template specialization 'std::__1::sort<_object *>' requested here 
    std::sort<PyObject *>(__pyx_t_2, __pyx_t_5); 
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3609:1: note: candidate template ignored: substitution failure [with _Compare = std::__1::__less<_object, _object> &, _ForwardIterator = _object *] 
__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3773:9: error: no matching function for call to '__sort3' 
     _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp); 
     ^~~~~~~~~~~~~~~~~~~~~~~~ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__config:366:15: note: expanded from macro '_VSTD' 
#define _VSTD std::_LIBCPP_NAMESPACE 
      ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:4003:32: note: in instantiation of function template specialization 'std::__1::__insertion_sort_incomplete<std::__1::__less<_object, _object> &, _object *>' 
     requested here 
      bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp); 
          ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:4048:5: note: in instantiation of function template specialization 'std::__1::__sort<std::__1::__less<_object, _object> &, _object *>' requested here 
    __sort<_Comp_ref>(__first, __last, __comp); 
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:4057:12: note: in instantiation of function template specialization 'std::__1::sort<_object *, std::__1::__less<_object, _object> >' requested here 
    _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 
     ^
sample.cpp:855:8: note: in instantiation of function template specialization 'std::__1::sort<_object *>' requested here 
    std::sort<PyObject *>(__pyx_t_2, __pyx_t_5); 
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3609:1: note: candidate template ignored: substitution failure [with _Compare = std::__1::__less<_object, _object> &, _ForwardIterator = _object *] 
__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3776:9: error: no matching function for call to '__sort4' 
     _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp); 
     ^~~~~~~~~~~~~~~~~~~~~~~~ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__config:366:15: note: expanded from macro '_VSTD' 
#define _VSTD std::_LIBCPP_NAMESPACE 
      ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3646:1: note: candidate template ignored: substitution failure [with _Compare = std::__1::__less<_object, _object> &, _ForwardIterator = _object *] 
__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3, 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3779:9: error: no matching function for call to '__sort5' 
     _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp); 
     ^~~~~~~~~~~~~~~~~~~~~~~~ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__config:366:15: note: expanded from macro '_VSTD' 
#define _VSTD std::_LIBCPP_NAMESPACE 
      ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3672:1: note: candidate template ignored: substitution failure [with _Compare = std::__1::__less<_object, _object> &, _ForwardIterator = _object *] 
__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3, 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3784:5: error: no matching function for call to '__sort3' 
    __sort3<_Compare>(__first, __first+1, __j, __comp); 
    ^~~~~~~~~~~~~~~~~ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3609:1: note: candidate template ignored: substitution failure [with _Compare = std::__1::__less<_object, _object> &, _ForwardIterator = _object *] 
__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c) 
^ 
8 errors generated. 
error: command 'cc' failed with exit status 1 
make: *** [all] Error 1 

Что я не делал право сортировать этот вектор?

+1

Как правило, было бы полезно, если бы вы предоставили код C++, который генерирует эти ошибки, даже если он сгенерирован. Это также поможет вам понять проблему –

ответ

0

Посмотрите на первое сообщение об ошибке: «недействительных операнды бинарного выражения („Const _object“и„Const _object“)» и линия его котировки косвенно из std::less, что функция сравнения по умолчанию при использовании std::sort. Причиной ошибки является то, что упорядоченная последовательность C++, по-видимому, состоит из _object экземпляров, а не целых чисел, как вы, кажется, предполагаете, и что для этого типа сравнение меньше, чем просто не определено.

+0

Но разве я не определял свой вектор для хранения 'int' типов? Не следует ли неявно определять компаратор? – hlin117

+1

Нет, 'int' по-прежнему является целым типом Python, который представлен как' _object' в C++. Проверьте http://docs.cython.org/src/userguide/fusedtypes.html, это 'cython.int', который вы хотите. –

+0

Спасибо за ваш вклад, Ульрих. Я изменил свой код, поэтому у меня есть 'vector [cython.int]()' и 'stdsort [cython.int]', но теперь у меня есть ошибка «Неверное количество аргументов шаблона: ожидается 2, получено 1». Я знаю, что он ожидает, что я предоставим здесь компаратор (придерживаясь определения другого 'std :: sort'), но есть ли способ обойти это? – hlin117

Смежные вопросы