我想使用a PriorityQueue在图形上进行拓扑排序.为简洁起见,我想为比较器使用匿名内部类.但是,我需要访问图表g以确定我正在查看的节点的程度.这可能吗?
/**
* topological sort
* @param g must be a dag
*/
public static Queue<String> topoSort(DirectedGraph<String, DefaultEdge> g) {
Queue<String> result = new PriorityQueue<String>(g.vertexSet().size(),
new Comparator<String>() {
DirectedGraph<String, DefaultEdge> g;
@Override
public int compare(String arg0, String arg1) {
if (g.inDegreeOf(arg0) < g.inDegreeOf(arg1)) {
return -1;
}
if (g.inDegreeOf(arg0) > g.inDegreeOf(arg1)) {
return 1;
}
return 0;
}
});
result.addAll(g.vertexSet());
return result;
}
Run Code Online (Sandbox Code Playgroud)
正确的代码
/**
* topological sort
* @param g must be a dag
*/ …Run Code Online (Sandbox Code Playgroud) 为什么C++不允许这样的东西呢?
我需要有多个优先级队列,其数量将在运行时确定.
这无法编译
std::vector<std::priorityqueue<Class A>>.
有更好的方法吗?
我有一个函数需要根据一些输入参数返回一个排序列表.我选择了一个std::priority_queue来保存这个列表.
但是编译器给了我一个我不认识的错误.这是我的代码:
struct DepthCompare {
bool operator()
(const struct inst *&lhs, const struct inst *&rhs) const
{
return lhs->depth < rhs->depth;
}
};
typedef priority_queue<struct inst*> HeuristicList;
HeuristicList getHeuristicList(struct BasicBlock &) {
HeuristicList ret( DepthCompare );
return ret;
}
Run Code Online (Sandbox Code Playgroud)
编译器conversion from 'HeuristicList (*)(DepthCompare)' to non-scalar type 'HeuristicList' requested 在return语句的行上说了一个.
它看起来不像我想要返回一个指针.出了什么问题?
我试图使用优先级队列来保持有序的整数列表.在这样一个简单的exaple:
PriorityQueue<Integer> queue = new PriorityQueue<>();
queue.offer(3000);
queue.offer(1999);
queue.offer(999);
for(Integer i : queue)
System.out.println(i);
Run Code Online (Sandbox Code Playgroud)
这打印
999
3000
1999
Run Code Online (Sandbox Code Playgroud)
这不是我期待的考虑自然的讽刺.
我只想迭代而不删除或添加通过队列(作为排序列表)与排序.我还能以简单的方式做到吗?
我希望能够在Collection中对对象进行老化.
例如,我可以将字符串放在HashSet中.
因此,我可以通过跟踪每个String然后运行清理Thread并从Collection中清除String(如果它早于某个时间量)来完成此操作.
但是我正在寻找:
谢谢.
我使用的priority_queue<unsigned long,vector<unsigned long>,greater<unsigned long> >是C++,内存限制为16MB.我的程序只需要10MB,但一旦达到8653464字节,它会尝试将其容量加倍并抛出一个bad_alloc.
有没有办法使用我当前的实现来阻止这个?可以在priority_queue具有数(n)时间仍然如果我切换到deque[从vector]
当我编写代码时,我对仿函数的使用感到困惑.
如果我想按降序对整数序列进行排序,我必须greater<int>输入第三个参数sort,例如sort(v.begin(), v.end(), greater<int>());.
但是在处理时priority_queue,我必须less<int>输入第三个参数来获得最大顶堆,例如priority_queue< int, vector <int>, less<int> > heap;.
所以我的问题是为什么我们使用两个不同的函子获得相同的提升关系?
任何人都可以解释为什么会这样吗?更好地使用STL实现的源代码让我清楚.
为我可怜的英语道歉.
非常感谢!
我有来自API时代的现有代码。这是一个基于MPCreateTask的线程。看起来我可以将其移到GCG队列中,但是有点麻烦。当前基于MPCreateQueue的三个队列用于三个优先级。
在GCD中,我已经弄清楚了,并测试了以下代码作为GCD重构的概念证明(天哪,我讨厌这个词,但很合适)。
首先,这是否可以实现我所期望的,所有动作(输入到例程的块)将是串行的。动作将具有由例程调度它们指定的优先级。
第二,是否有更好的方法可以做到这一点?
// set up three serial queues
dispatch_queue_t queueA = dispatch_queue_create("app.queue.A" , DISPATCH_QUEUE_SERIAL);
dispatch_queue_t queueB = dispatch_queue_create("app.queue.B" , DISPATCH_QUEUE_SERIAL);
dispatch_queue_t queueC = dispatch_queue_create("app.queue.C" , DISPATCH_QUEUE_SERIAL);
// set the target queues so that all blocks posted to any of the queues are serial
// ( the priority of the queues will be set by queueC.
dispatch_set_target_queue( queueB, queueC ) ;
dispatch_set_target_queue( queueA, queueB ) ;
void lowPriorityDispatch( dispatch_block_t lowAction )
{
dispatch_async( queueC, ^{
lowAction() ;
}) ; …Run Code Online (Sandbox Code Playgroud) 以下给出了下面提到的代码的错误.哪里出错了?
error: ‘function’ is not a member of ‘std’
Run Code Online (Sandbox Code Playgroud)
我想使用C++ std lib队列进行优先级队列,最小化队列是IceCream,它花费的时间最少.我试过实现这个 - > 用自定义比较器在c ++中声明一个priority_queue
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <iterator>
#include <string>
#include <sstream>
using namespace std;
class IceCream
{
public:
int time_to_prep;
IceCream(int flav) {
time_to_prep = flav;
}
};
bool Compare(IceCream a, IceCream b)
{
return a.time_to_prep > b.time_to_prep;
}
int main()
{
priority_queue<IceCream, vector<IceCream>, std::function<bool(IceCream a, IceCream b)> > my_pq(Compare);
my_pq.push(IceCream(4)); …Run Code Online (Sandbox Code Playgroud) 我正在从cppreference中读取代码示例:
#include <functional>
#include <queue>
#include <vector>
#include <iostream>
template<typename T> void print_queue(T& q) {
while(!q.empty()) {
std::cout << q.top() << " ";
q.pop();
}
std::cout << '\n';
}
int main() {
std::priority_queue<int> q;
for(int n : {1,8,5,6,3,4,0,9,7,2})
q.push(n);
print_queue(q);
std::priority_queue<int, std::vector<int>, std::greater<int> > q2;
for(int n : {1,8,5,6,3,4,0,9,7,2})
q2.push(n);
print_queue(q2);
// Using lambda to compare elements.
auto cmp = [](int left, int right) { return (left ^ 1) < (right ^ 1);};
std::priority_queue<int, std::vector<int>, decltype(cmp)> q3(cmp);
for(int …Run Code Online (Sandbox Code Playgroud)