在向量中搜索队列大小

search the queue sizes in a vector

本文关键字:队列 搜索 向量      更新时间:2023-10-16

我有一个范例,在这个范例中,之前的整数被排队到向量中的一个队列,搜索队列的循环,并将整数排队到队列中大小最小的队列。下面的代码显示了操作。

#include <vector> 
#include <queue> 
std::vector<std::queue<int> > q
int min_index = 0;
std::size_t size = q.size();
for( i=0; i<size; i++){ //accessing loop of queues
    if(q[min_index].size() > q[i].size())
        min_index = i; // Now q[min_index] is the shortest queue
} 
q[min_index].push(int)

接下来,我试图用以下条件来扩展我的范例,即整数应该被排队到最短的队列,直到它成为队列中的最大大小。

do{
   q[min_index].push(int)
} while(q[min_index].size > queue sizes in the vector loop except this queue )

如何搜索除此队列之外的队列大小循环有什么想法请帮忙!!

只需计算最大队列大小和最小索引:

int min_index = 0;
int max_size = -1;
std::size_t size = q.size();
for( i=0; i<size; i++){ //accessing loop of queues
    if(q[min_index].size() > q[i].size())
        min_index = i; // Now q[min_index] is the shortest queue
    if(q[i].size() > max_size)
        max_size = q[i].size(); // longest queue
} 
while(q[min_index].size < max_size)
{
    q[min_index].push(int);
}