0

我需要比较 10 个队列的大小并确定最小的一个以插入下一个元素

创建正常的 if 语句将需要很多情况

那么有没有办法使用队列队列或队列数组来做到这一点?

注意:我需要在 2 种情况下基于 2 个不同的事物来比较我的队列 1-基于大小(其中的节点数) 2-基于其中节点中的数据总数(我有一个单独的计算函数)

4

3 回答 3

1

您应该考虑使用堆,其中关键是每个队列的大小。

http://en.wikipedia.org/wiki/Heap_%28data_structure%29

于 2010-01-08T17:22:25.453 回答
0

最简单的方法是队列向量。遍历向量以找到具有最少条目的队列。

于 2010-01-08T17:28:20.903 回答
0

你可以做这样的事情

std::queue<int> queue1;
std::vector<std::queue<int> > queues; // Declare a vector of queue 

queues.push_back(queue1);       // Add all of your queues to the vector 
// insert other queue here ... 

std::vector<std::queue<int> >::const_iterator minItt = queues.begin(); // Get the first queue in the vector 

// Iterate over all of the queues in the vector to fin the one with the smallest size 
for(std::vector<std::queue<int> >::const_iterator itt = ++minItt; itt != queues.end(); ++itt) 
{ 
    if(itt->size() < minItt->size()) 
        minItt = itt; 
} 

如果它对您来说不够快,您始终可以使用 std::for_each() 和仿函数在向量中进行搜索。

于 2010-01-08T17:37:03.397 回答