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!!
@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
@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.
@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?
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.