我怎样才能比较cpp中的队列?

时间:2010-01-08 17:11:51

标签: c++ arrays visual-c++ queue priority-queue

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

创建正常的if语句将需要很多案例

所以有没有办法使用队列队列或队列数组?

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

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

答案 2 :(得分: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()和一个仿函数在向量中进行搜索。

相关问题