问题遇到的现象和发生背景
我仿照官网上的大根堆代码写了一个小根堆代码,结果报错了
问题相关代码,请勿粘贴截图
官方代码
std::vector<int> vec={3, 1, 4, 1, 5};
std::priority_queue<int> c3(std::less<int>(), vec);
std::cout << c3.size() << '\n';
我的代码
std::priority_queue<int> q(std::greater<int>(),hand);
运行结果及报错内容
Line 6: Char 34: error: no matching constructor for initialization of 'std::priority_queue<int>'
std::priority_queue<int> q(std::greater<int>(),hand);
^ ~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_queue.h:505:7: note: candidate constructor not viable: no known conversion from 'std::greater<int>' to 'const std::less<int>' for 1st argument
priority_queue(const _Compare& __x, const _Sequence& __s)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_queue.h:510:7: note: candidate constructor not viable: no known conversion from 'std::greater<int>' to 'const std::less<int>' for 1st argument
priority_queue(const _Compare& __x, _Sequence&& __s = _Sequence())
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_queue.h:584:2: note: candidate template ignored: deduced conflicting types for parameter '_InputIterator' ('std::greater<int>' vs. 'std::vector<int, std::allocator<int>>')
priority_queue(_InputIterator __first, _InputIterator __last,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_queue.h:520:2: note: candidate template ignored: requirement 'uses_allocator<std::vector<int, std::allocator<int>>, std::vector<int, std::allocator<int>>>::value' was not satisfied [with _Alloc = std::vector<int, std::allocator<int>>]
priority_queue(const _Compare& __x, const _Alloc& __a)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_queue.h:537:2: note: candidate template ignored: requirement 'uses_allocator<std::vector<int, std::allocator<int>>, std::vector<int, std::allocator<int>>>::value' was not satisfied [with _Alloc = std::vector<int, std::allocator<int>>]
priority_queue(const priority_queue& __q, const _Alloc& __a)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_queue.h:541:2: note: candidate template ignored: requirement 'uses_allocator<std::vector<int, std::allocator<int>>, std::vector<int, std::allocator<int>>>::value' was not satisfied [with _Alloc = std::vector<int, std::allocator<int>>]
priority_queue(priority_queue&& __q, const _Alloc& __a)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_queue.h:516:2: note: candidate constructor template not viable: requires single argument '__a', but 2 arguments were provided
priority_queue(const _Alloc& __a)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_queue.h:443:11: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 2 were provided
class priority_queue
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_queue.h:443:11: note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 2 were provided
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_queue.h:526:2: note: candidate constructor template not viable: requires 3 arguments, but 2 were provided
priority_queue(const _Compare& __x, const _Sequence& __c,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_queue.h:532:2: note: candidate constructor template not viable: requires 3 arguments, but 2 were provided
priority_queue(const _Compare& __x, _Sequence&& __c, const _Alloc& __a)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_queue.h:501:2: note: candidate constructor template not viable: requires 0 arguments, but 2 were provided
priority_queue()
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_queue.h:573:2: note: candidate constructor template not viable: requires 4 arguments, but 2 were provided
priority_queue(_InputIterator __first, _InputIterator __last,
^
1 error generated.
我的解答思路和尝试过的方法
我想要达到的结果
建立小根堆