compare queue sizes in a vector

Feb 15, 2013 at 11:08am
I have a paradigm where a integer before gets enqueued to a queue in a vector, the loop of queues is searched and integer is enqueued to a queue which has minimum size among the queues. the following code shows the operation

1
2
3
4
5
6
7
8
9
10
#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)


next i am trying to extend my paradigm with the condition, that the integers should be enqueued to the shortest queue until the count of the shortest queue is less than or equal to count of any another queues in the loop of queues.

i want to do something like the code shown below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#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
//p=std::size(q.begin(),q.end(),size);
}
//while(q[min_index].size <= any other queue size in the loop leaving this shortest queue)
{
    q[min_index].push(int);
}


any ideas to implement this logic, will be grateful!!
Last edited on Feb 15, 2013 at 11:18am
Feb 15, 2013 at 11:51am
Is the question clear??
Feb 15, 2013 at 6:49pm
You could give an example.

start:
1
2
{}
{1,2,3}
end:
1
2
{42,42,42}
{1,2,3}
so you need to push the number `min2 - min' times. (where min2 is the second smallest number)
Feb 18, 2013 at 9:48am
@ne555
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <vector> 
#include <queue> 
std::size_t min_index = 0;
std::size_t second_shortest_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()) {
        second_shortest_index = min_index; 
        min_index = i; // Now q[min_index] is the shortest queue
    } else if (q[second_shortest_index].size() > q[i].size()) {
        second_shortest_index = min_index;
    }
} 
while(q[min_index].size <= q[second_shortest_index].size() )
    {
        q[min_index].push(int);

}


will this code work for the paradigm i said above
Feb 18, 2013 at 7:06pm
No, line 11 is incorrect
Topic archived. No new replies allowed.