为priority_queue定义自己的比较器

时间:2018-02-18 06:33:38

标签: c++

我正在尝试制作我自己的比较器最小优先级队列:

#include<iostream>
#include <queue>
#include <vector>
using namespace std;

struct ele{
    int data;
    int i,j;
};

struct mycomp
{
    bool operator () (const ele& lhs, const ele& rhs) const
    {
        return lhs.data > rhs.data;
    }
}
int main()
{

    int arr[] = { 1,3, 4, 5};
    int size = *(&arr + 1) - arr;
    cout<<size<<endl;
    priority_queue<ele, std::vector<ele> , mycomp> pq(arr, arr+size);
    return 0;
}

但它没有编译。你能帮忙吗?我也在operator <中使用struct ele进行了尝试,但也没有编译。

错误消息:

prog.cpp:17:5: error: expected ';' after struct definition
     }
     ^
In file included from /usr/include/c++/5/bits/char_traits.h:39:0,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/5/bits/stl_algobase.h: In instantiation of 'static _OI std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II = int*; _OI = ele*]':
/usr/include/c++/5/bits/stl_algobase.h:402:44:   required from '_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = int*; _OI = ele*]'
/usr/include/c++/5/bits/stl_algobase.h:438:45:   required from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = int*; _OI = __gnu_cxx::__normal_iterator<ele*, std::vector<ele> >]'
/usr/include/c++/5/bits/stl_algobase.h:471:8:   required from '_OI std::copy(_II, _II, _OI) [with _II = int*; _OI = __gnu_cxx::__normal_iterator<ele*, std::vector<ele> >]'
/usr/include/c++/5/bits/vector.tcc:637:16:   required from 'void std::vector<_Tp, _Alloc>::_M_range_insert(std::vector<_Tp, _Alloc>::iterator, _ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = int*; _Tp = ele; _Alloc = std::allocator<ele>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<ele*, std::vector<ele> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = ele*]'
/usr/include/c++/5/bits/stl_vector.h:1377:19:   required from 'void std::vector<_Tp, _Alloc>::_M_insert_dispatch(std::vector<_Tp, _Alloc>::iterator, _InputIterator, _InputIterator, std::__false_type) [with _InputIterator = int*; _Tp = ele; _Alloc = std::allocator<ele>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<ele*, std::vector<ele> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = ele*]'
/usr/include/c++/5/bits/stl_vector.h:1099:22:   required from 'std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::insert(std::vector<_Tp, _Alloc>::const_iterator, _InputIterator, _InputIterator) [with _InputIterator = int*; <template-parameter-2-2> = void; _Tp = ele; _Alloc = std::allocator<ele>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<ele*, std::vector<ele> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = ele*; std::vector<_Tp, _Alloc>::const_iterator = __gnu_cxx::__normal_iterator<const ele*, std::vector<ele> >; typename __gnu_cxx::__alloc_traits<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type>::const_pointer = const ele*]'
/usr/include/c++/5/bits/stl_queue.h:462:4:   required from 'std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(_InputIterator, _InputIterator, const _Compare&, _Sequence&&) [with _InputIterator = int*; _Tp = ele; _Sequence = std::vector<ele>; _Compare = mycomp]'
prog.cpp:24:72:   required from here
/usr/include/c++/5/bits/stl_algobase.h:340:18: error: no match for 'operator=' (operand types are 'ele' and 'int')
        *__result = *__first;
                  ^
prog.cpp:6:12: note: candidate: constexpr ele& ele::operator=(const ele&)
     struct ele{
            ^
prog.cpp:6:12: note:   no known conversion for argument 1 from 'int' to 'const ele&'
prog.cpp:6:12: note: candidate: constexpr ele& ele::operator=(ele&&)
prog.cpp:6:12: note:   no known conversion for argument 1 from 'int' to 'ele&&'
In file included from /usr/include/c++/5/deque:62:0,
                 from /usr/include/c++/5/queue:60,
                 from prog.cpp:2:
/usr/include/c++/5/bits/stl_construct.h: In instantiation of 'void std::_Construct(_T1*, _Args&& ...) [with _T1 = ele; _Args = {int&}]':
/usr/include/c++/5/bits/stl_uninitialized.h:75:18:   required from 'static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = int*; _ForwardIterator = ele*; bool _TrivialValueTypes = false]'
/usr/include/c++/5/bits/stl_uninitialized.h:126:15:   required from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = int*; _ForwardIterator = ele*]'
/usr/include/c++/5/bits/stl_uninitialized.h:281:37:   required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = int*; _ForwardIterator = ele*; _Tp = ele]'
/usr/include/c++/5/bits/vector.tcc:643:34:   required from 'void std::vector<_Tp, _Alloc>::_M_range_insert(std::vector<_Tp, _Alloc>::iterator, _ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = int*; _Tp = ele; _Alloc = std::allocator<ele>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<ele*, std::vector<ele> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = ele*]'
/usr/include/c++/5/bits/stl_vector.h:1377:19:   required from 'void std::vector<_Tp, _Alloc>::_M_insert_dispatch(std::vector<_Tp, _Alloc>::iterator, _InputIterator, _InputIterator, std::__false_type) [with _InputIterator = int*; _Tp = ele; _Alloc = std::allocator<ele>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<ele*, std::vector<ele> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = ele*]'
/usr/include/c++/5/bits/stl_vector.h:1099:22:   required from 'std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::insert(std::vector<_Tp, _Alloc>::const_iterator, _InputIterator, _InputIterator) [with _InputIterator = int*; <template-parameter-2-2> = void; _Tp = ele; _Alloc = std::allocator<ele>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<ele*, std::vector<ele> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = ele*; std::vector<_Tp, _Alloc>::const_iterator = __gnu_cxx::__normal_iterator<const ele*, std::vector<ele> >; typename __gnu_cxx::__alloc_traits<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type>::const_pointer = const ele*]'
/usr/include/c++/5/bits/stl_queue.h:462:4:   required from 'std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(_InputIterator, _InputIterator, const _Compare&, _Sequence&&) [with _InputIterator = int*; _Tp = ele; _Sequence = std::vector<ele>; _Compare = mycomp]'
prog.cpp:24:72:   required from here
/usr/include/c++/5/bits/stl_construct.h:75:7: error: no matching function for call to 'ele::ele(int&)'
     { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
       ^
prog.cpp:6:12: note: candidate: ele::ele()
     struct ele{
            ^
prog.cpp:6:12: note:   candidate expects 0 arguments, 1 provided
prog.cpp:6:12: note: candidate: constexpr ele::ele(const ele&)
prog.cpp:6:12: note:   no known conversion for argument 1 from 'int' to 'const ele&'
prog.cpp:6:12: note: candidate: constexpr ele::ele(ele&&)
prog.cpp:6:12: note:   no known conversion for argument 1 from 'int' to 'ele&&'

2 个答案:

答案 0 :(得分:5)

arrint的数组,您正在尝试使用它来构建std::priority_queue ele。由于没有从int转换为ele,因此无效。如果要使其工作,您需要在ele中定义一个接受int参数的构造函数。

答案 1 :(得分:-2)

工作代码:

#include<iostream>
#include <queue>
#include <vector>
using namespace std;

struct ele{
    public:
    int data;
    ele(int i) : data(i)
    {
    }
};

struct mycomp
{
    bool operator () (const ele& lhs, const ele& rhs)
    {
        return lhs.data > rhs.data;
    }
};


int main()
{

    int arr[] = { 1,3, 4, 5};
    int size = *(&arr + 1) - arr;
    cout<<size<<endl;
    priority_queue<ele,vector<ele> , mycomp> pq(arr, arr+1);
    cout<<pq.top().data;
    return 0;
}