gpt4 book ai didi

c++ - 通过引用传入优先队列

转载 作者:行者123 更新时间:2023-12-03 07:04:35 26 4
gpt4 key购买 nike

我的 main.cpp 中有一个带有自定义比较器的对象优先级队列 vector 。我有多个需要优先队列并且应该修改优先队列的函数。通过引用传递优先级队列的正确方法是什么?它会使用指针吗?我对如何去做这件事感到非常困惑。

在我的主要声明中,我声明了我的优先级队列,例如:
类型是订单vector<priority_queue<Order, vector<Order>, compLower> > buy_orders (num_equities);
我调用了一个函数 makeTransaction ,它只处理各个优先级队列:
makeTransaction(equity_id, buy_orders[equity_id]);
但是,当我调用此函数并稍后尝试访问 buy_orders , 没有什么变化。我怎样才能通过引用传递它makeTransaction可以对 buy_orders 进行必要的更改?

另外,我将如何适本地声明 makeTransaction 函数?

最佳答案

例子 :

void my_function(std::priority_queue<MyObject>& q)
{
while(q.size() >= 10)
{
MyObject& o = q.top();
// Do something with o
q.pop();
}
}

// Call the function :
std::priority_queue<MyObject> my_queue;
// fill the queue
my_function(my_queue); // Here, my_queue will be modified

关于c++ - 通过引用传入优先队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29004868/

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