views:

72

answers:

3

hi there 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 )

thanks in advanced regards ahmed

+1  A: 

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

Brian L
A: 

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

anon
A: 

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.

Frank
so assumin that my queues are A,B,C,D,Cwill the minItt returns the queue it self ( ie . A ) or will it return its value ?
ahmed
'minItt' is an iterator over the vector of queue. You can use it as a pointer (with the '->' operator) and have access to your queue that way.
Frank