search the queue sizes in a vector

hello, i have a paradigm where a integer before gets enqueued to a queue, the loop of queues in a vector 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 that the condition the integers should be enqueued to the shortest queue until it becomes maximum size among the queues.
1
2
3
do{
   q[min_index].push(int)
} while(q[min_index].size > queue sizes in the vector loop except this queue )

how to search the loop of queues of vector in the while ()
any ideas please help!!
Last edited on
It is better in the first loop to seek the minimum and the maximum simultaneously.

By the way there is standard algorithm std::minmax_element that can be used in your task.
Last edited on
@vlad from moscow where should i use std::minmax_element, in the For loop? can you please explain a bit more detailed?
Last edited on
Here is a code that is compiled with GCC 4.7.2 though I did not test ii.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <vector>
#include <queue>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <ctime>
 
int main()
{
    const size_t N = 5;
    std::vector<std::queue<int>> v( N );
    
    std::srand( std::time( 0 ) );
    for ( auto &q : v )
    {
        size_t k = std::rand() % N;
        for ( size_t i = 0; i < k; i++ ) q.push( std::rand() % N );
    }
    
    auto p = std::minmax_element( v.begin(), v.end(), 
                                  []( const std::queue<int> &q1, const std::queue<int> &q2 ) 
                                  { return ( q1.size() < q2.size() ); } );
                                  
    
    while ( ( p.first )->size() <= ( p.second )->size() ) ( p.first )->push( std::rand() % N );
    
  
    return 0;
}


You can try this code on-line at www.ideone.com
Last edited on
I have tested the code by substituting the std::queue for std::list that it would be easy to output elements. Here is the result

Original vector:
4 4 4
3 4 4
0
4
3 1 3 2

Resulted vector after increasing the minimal list:
4 4 4
3 4 4
0 2 3 1 1
4
3 1 3 2
Last edited on
@vlad from Moscow wow great it worked..! thanks much
@vlad from Moscow, i implemented the same logic in the looping way, as shown below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <vector> 
#include <queue> 
int min_index = 0;
int max_size = -1;
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
    if(q[i].size() > max_size)
        max_size = q[i].size(); // longest queue
} 
while(q[min_index].size < max_size)
{
    q[min_index].push(int);
}


bt nw i am trying to do the logic with another 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
15
#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);

}


how to implement this in a same looping way? could please help me out

thanks!!
Last edited on
I have not understood what you want. Why do not use the following loop?

1
2
3
4
while ( q[min_index].size() <= q[some_other_index].size() )
{
   q[min_index].push( SomeValue );
}
Last edited on
@vlad from moscow On the whole i need a paradigm, that every time the integer should be enqueued to the shortest queue among the loop of queues of the vector(which is done in the 1st code of my question), additionally, the integers should get continued to enqueue in the shortest queue while the condition is true that the shortest queue's size is less than or equal to any another queue's size in the loop of queues.

as u said above

1
2
3
4
while ( q[min_index].size() <= q[some_other_index].size() )
{
   q[min_index].push( SomeValue );
}


will the q[some_other_index].size() has all other the queue sizes except the shortest queue's size?
Last edited on
In this case I think you should find two successive minimums.
@vlad from moscow so i need to find a another minimum and compare in the while loop? can you please explain a more detailed
Last edited on
@vlad from moscow I hope we can use successive sorting method or std::partial_sort to find the successive minimum values, can you explain how to sort in this loop?
Last edited on
Presumably you know the number of items you're going to be adding. Calculate the minimum queue size you'll have after adding them. Iterate through your container of queues in order of smallest to largest sized queue, adding enough elements to each queue to bring it up to the minimum size, until either all queues have reached the minimum size or you run out of items to enqueue.

If you haven't distributed all of the items to be enqueued yet, iterate over the queue adding 1 item to each queue until you have no more items to add.

Example code here: https://gist.github.com/cire3791/4963453#file-queuemanip-cpp
Last edited on
@cire
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 one work with a simple looping?
Last edited on
Topic archived. No new replies allowed.