开发者

How can i compare queues in cpp?

i need to compare the size of 10 queues and determine the least one in size to insert the next element in

creating normal if statements will take A LOT of cases

so is there any way to do it using a queue of queue for example or an array of queues ?

note : i will need to compare my queues based on 2 separate things in开发者_如何转开发 2 situations 1- based on size ( number of nods in it ) 2- based on the total number of the data in the nods in it ( which i have a separate function to calculate )


You should look into using a heap, where the key is the size of each queue.

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


You could do something like that

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; 
} 

If it's not fast enough for you, you could always make your search in the vector with std::for_each() and a functor.


The simplest approach is a vector of queues. Iterate through the vector to find the queue with the fewest entries.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜