我正在尝试使用std :: sort对向量进行排序,即

ScanIndex::ScanIndex(std::vector<ScanData*> *scans, int currVersion, KeyCell minKey, KeyCell maxKey){
        std::sort(scans->begin(), scans->end(),
         [](const ScanData *& a, const ScanData *& b) -> bool
         {
             return (a->version.load() > b->version.load());
         });
}


出现此错误时:



/usr/include/c++/5/bits/predefined_ops.h: In instantiation of ‘constexpr bool __gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2) [with _Iterator1 = __gnu_cxx::__normal_iterator<ScanData**, std::vector<ScanData*> >; _Iterator2 = __gnu_cxx::__normal_iterator<ScanData**, std::vector<ScanData*> >; _Compare = ScanIndex::ScanIndex(std::vector<ScanData*>*, int, KeyCell, KeyCell)::<lambda(const ScanData*&, const ScanData*&)>]’:
/usr/include/c++/5/bits/stl_algo.h:1842:14:   required from ‘void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ScanData**, std::vector<ScanData*> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<ScanIndex::ScanIndex(std::vector<ScanData*>*, int, KeyCell, KeyCell)::<lambda(const ScanData*&, const ScanData*&)> >]’
/usr/include/c++/5/bits/stl_algo.h:1880:25:   required from ‘void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ScanData**, std::vector<ScanData*> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<ScanIndex::ScanIndex(std::vector<ScanData*>*, int, KeyCell, KeyCell)::<lambda(const ScanData*&, const ScanData*&)> >]’
/usr/include/c++/5/bits/stl_algo.h:1966:31:   required from ‘void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<ScanData**, std::vector<ScanData*> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<ScanIndex::ScanIndex(std::vector<ScanData*>*, int, KeyCell, KeyCell)::<lambda(const ScanData*&, const ScanData*&)> >]’
/usr/include/c++/5/bits/stl_algo.h:4729:18:   required from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<ScanData**, std::vector<ScanData*> >; _Compare = ScanIndex::ScanIndex(std::vector<ScanData*>*, int, KeyCell, KeyCell)::<lambda(const ScanData*&, const ScanData*&)>]’
/home/dvir/CLionProjects/KiWi-cpp-pq-port/ScanIndex.cpp:38:11:   required from here
/usr/include/c++/5/bits/predefined_ops.h:125:46: error: no match for call to ‘(ScanIndex::ScanIndex(std::vector<ScanData*>*, int, KeyCell, KeyCell)::<lambda(const ScanData*&, const ScanData*&)>) (ScanData*&, ScanData*&)’
         { return bool(_M_comp(*__it1, *__it2)); }


这是ScanData对象



class ScanData{
public:
    static const ScanData* empty_ScanData;

    ScanData(KeyCell min, KeyCell max) : min(min), max(max), version(0)
    {}

    ScanData(const ScanData& scanData) : min(scanData.min), max(scanData.max), version(version.load())
    {}

    std::atomic<int> version;
    KeyCell min;
    KeyCell max;

};


我猜想我声明的lambda类型(签名)与预期的不同,但它似乎与文档中的签名相对应。
有什么想法吗?

最佳答案

scans是指向包含ScanData*的向量的指针。

因此,lambda可以期望一个可能绑定到ScanData* const &的参数。

您指定的类型是const ScanData* &(所引用的指针不是const,而是pointee)。资格不匹配。从ScanData*const ScanData*的转换是可能的,但这将需要一个临时指针,并且非常量左值引用不能绑定到一个。

由于指针是值类型,并且廉价地复制值类型,因此不要通过引用传递。将指针按值传递给lambda。

     [](const ScanData *a, const ScanData *b) -> bool
     {
         return (a->version.load() > b->version.load());
     });

09-28 01:53