为Priority_queue定义自己的比较器
define own comparator for priority_queue
我正在尝试制作自己的比较器最低优先级队列:
#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;
}
,但它不是编译。你能帮忙吗?我还尝试了struct ele
中的operator <
,但这也不编译。
错误消息:
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&&'
arr
是int
的数组,您正在尝试使用它来构造ele
的std::priority_queue
。由于从int
到ele
没有转换,这将行不通。您需要在ele
中定义一个构造函数,该构造函数如果要使它起作用,则需要接受int
参数。
工作代码:
#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;
}
相关文章:
- 将 std::set 与基于键的比较器一起使用
- C++复杂情况的比较器通过参数问题
- "operator()"在重载运算符方法中是什么意思,在priority_queue(STL)中用作C++中的比较器?
- C++设置了一个用于排序的比较器和另一个用于唯一性的比较器
- std::为插入和排序设置不同的比较器
- 排序函数和优先级队列 C++ 中的比较器
- 下面的比较器函数在 c++ 中如何工作?
- 使用更大的比较器初始化映射
- Qsort和不兼容的比较器功能 - C++
- sort() 方法 c++ 中的比较器函数.为大量数字获得不同的解决方案
- 排序、专业化的比较器
- 为Priority_queue定义自己的比较器
- 如何使用Imebra库将压缩像素(用我自己的编码器压缩)回到DICOM图像文件中
- 如何使用将 const int 的运算符用作 int* 优先级队列的比较器
- C++函数来返回基于参数的比较器通用映射类型?
- 关联容器所需的比较器
- 我应该为地图中的两个智能指针对制作自己的比较器吗?
- C++ 集<>类对象。使用自己的比较器给出错误:C2804:二进制'operator <'参数过多
- C++ 集<>类对象。使用自己的比较器给出来自功能,xtree等的错误 JUNK
- 使用自己的比较器运算符()进行映射<>。在找不到 KEY 的情况下给出错误