gpt4 book ai didi

c++ - 增加队列大小并找到最短队列

转载 作者:行者123 更新时间:2023-11-30 02:00:34 25 4
gpt4 key购买 nike

我有一个范例,在 vector 的队列循环中,如果第 i 个队列的条件为真,则将第 i 个队列的队列大小增加 5 。完成此操作后,我需要搜索所有队列的队列大小并在最短队列中排队。我想做一些类似下面给出的代码

#include <vector> 
#include <queue>
int min_index = 0;
std::vector<std::queue<int> > q
std::size_t size = q.size();
for( i=0; i<size; i++){
if(..) {// A condition is true
//increase the size of the ith queue by 5 more times
}

if(q[min_index].size() > q[i].size())
min_index = i; // Now q[min_index] is the shortest queue
}
q[min_index].push(int)
}

如果条件为真,如何人为地增加队列大小?然后搜索队列并找到最短大小的队列。

已更新

#include <vector> 
#include <deque>
int min_index = 0;
std::vector<std::deque<int> > q
std::size_t size = q.size();
for( i=0; i<size; i++){
if(...) {// A condition is true
q[i].resize(q[i].size() + 5)
}
if(q[min_index].size() > q[i].size())
min_index = i; // Now q[min_index] is the shortest queue
}
q[min_index].push(int)
}

最佳答案

不清楚“增加队列大小”是什么意思。

如果您的意思是“增加队列的容量”,则不需要。队列的默认底层容器是 deque ,它不是内存中的连续 block ,因此不会遇到任何扩展问题,无需提前 reserve() 。参见 here有关这方面的更多详细信息。

因此,队列的大小就是其中的项目数。如果你想增加 that,deque 有一个 resize() 函数,它要么将所有新项目的指定值作为参数,要么只是对它们进行值初始化.

关于c++ - 增加队列大小并找到最短队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14984333/

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