gpt4 book ai didi

c++ - 如何清除使用用户定义比较的 `priority_queue`?

转载 作者:行者123 更新时间:2023-12-02 15:47:38 26 4
gpt4 key购买 nike

如何清除使用用户定义比较的priority_queue

来自 std::priority_queue documentation ,我将 priority_queue 的使用减少到我需要的情况(= 具有用户定义比较的队列)

>> cat test.cpp 
#include <functional>
#include <queue>
#include <vector>
#include <iostream>
#include <utility>

auto queue_cmp = [](std::pair<int, double> const& lhs,
std::pair<int, double> const& rhs) {
return lhs.second > rhs.second; // Custom order.
};
typedef std::priority_queue<std::pair<int, double>,
std::vector<std::pair<int, double>>,
decltype(queue_cmp)> custom_queue;

template<typename T>
void print_queue(T q) { // NB: pass by value so the print uses a copy
int s = 0;
while(!q.empty()) {
std::pair<int, double> elem = q.top();
std::cout << s << ": " << elem.first << ", " << elem.second << std::endl;
q.pop();
s++;
}
std::cout << '\n';
}

int main() {
custom_queue q(queue_cmp);
for (int n = 10; n < 20; n++) {
double val = static_cast <double>(rand())/(static_cast<double>(RAND_MAX));
q.push(std::pair<int, double>(n, val));
}
print_queue(q);
//q = custom_queue(queue_cmp);
}

运行正常

>> g++ -o test test.cpp 

>> ./test
0: 15, 0.197551
1: 18, 0.277775
2: 16, 0.335223
3: 11, 0.394383
4: 19, 0.55397
5: 17, 0.76823
6: 12, 0.783099
7: 13, 0.79844
8: 10, 0.840188
9: 14, 0.911647

现在我需要重置q,所以,按照priority queue clear method ,我取消注释 test.cpp 中的最后一行......并得到这个编译错误,就好像我得到了正确的一样,在 lamdba 上删除了复制构造函数:

>> g++ -o test test.cpp 
test.cpp: In function ‘int main()’:
test.cpp:34:31: error: use of deleted function ‘std::priority_queue<std::pair<int, double>, std::vector<std::pair<int, double> >, <lambda(const std::pair<int, double>&, const std::pair<int, double>&)> >& std::priority_queue<std::pair<int, double>, std::vector<std::pair<int, double> >, <lambda(const std::pair<int, double>&, const std::pair<int, double>&)> >::operator=(std::priority_queue<std::pair<int, double>, std::vector<std::pair<int, double> >, <lambda(const std::pair<int, double>&, const std::pair<int, double>&)> >&&)’
34 | q = custom_queue(queue_cmp);
| ^
In file included from /usr/include/c++/12/queue:64,
from test.cpp:2:
/usr/include/c++/12/bits/stl_queue.h:498:11: note: ‘std::priority_queue<std::pair<int, double>, std::vector<std::pair<int, double> >, <lambda(const std::pair<int, double>&, const std::pair<int, double>&)> >& std::priority_queue<std::pair<int, double>, std::vector<std::pair<int, double> >, <lambda(const std::pair<int, double>&, const std::pair<int, double>&)> >::operator=(std::priority_queue<std::pair<int, double>, std::vector<std::pair<int, double> >, <lambda(const std::pair<int, double>&, const std::pair<int, double>&)> >&&)’ is implicitly deleted because the default definition would be ill-formed:
498 | class priority_queue
| ^~~~~~~~~~~~~~
/usr/include/c++/12/bits/stl_queue.h:498:11: error: use of deleted function ‘<lambda(const std::pair<int, double>&, const std::pair<int, double>&)>&<lambda(const std::pair<int, double>&, const std::pair<int, double>&)>::operator=(const<lambda(const std::pair<int, double>&, const std::pair<int, double>&)>&)’
test.cpp:7:19: note: a lambda closure type has a deleted copy assignment operator
7 | auto queue_cmp = [](std::pair<int, double> const& lhs,
| ^

在这种情况下有没有办法重置q

编辑


bool queue_cmp(std::pair<int, double> const& lhs,
std::pair<int, double> const& rhs) {
return lhs.second > rhs.second; // Custom order.
};


没有帮助

最佳答案

不在 C++17 中。

C++20 为无捕获 lambda 提供了默认构造函数。但是在早期的语言版本中,构造函数被删除了。

真的,你不应该使用 lambda。只需使用命名结构。

关于c++ - 如何清除使用用户定义比较的 `priority_queue`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73612303/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com