increase sizes of the queue in a vector of queues and find shortest queue

i have a paradigm in a loop of queues of a vector,if a condition is true,increase sizes of the queue of that particular queue in the loop of queues, if condition is false, the queuesize is left as such in loop of queues. After this operation i need to search the queue sizes of all queues and enqueue in the shortest queue.
i want to do something like the code given below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <vector> 
#include <queue> 
int min_index = 0;
std::vector<std::queue<int> > q
std::size_t size = q.size();
for( i=0; i<size; i++){ 
    if(..) {// some condition is true
 q[i].size=q[i].size+5
}
else{
q[i].size=q[i].size+0
}
if(q[min_index].size() > q[i].size())
        min_index = i; // Now q[min_index] is the shortest queue
} 
q[min_index].push(int)
}


can you please help me out how to implement this logic?

will q[i].size=q[i].size+5 increase the queuesize by 5 for the ith queue?
Last edited on
@coder777 can you please me help me with the above one?
Unlike vector queue has no resize(). I wonder what sense does it make to artificially increase the size?

you can always use a loop:
1
2
for(int j = 0; j < 5; j++)
  q[i].push(0);


What are you trying to do on line 10/11? Currently it doesn nothing
@coder777 shall i use while loop () instead
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <vector> 
#include <queue> 
int min_index = 0;
std::vector<std::queue<int> > q
std::size_t size = q.size();
for( i=0; i<size; i++){ 
    while (..) {// some condition is true
  for(int j = 0; j < 5; j++)
  q[i].push(0);
}
if(q[min_index].size() > q[i].size())
        min_index = i; // Now q[min_index] is the shortest queue
} 
q[min_index].push(int)
}


will the above code, does the paradigm what i need?

Last edited on
well, i think the task is not to add a value to the queue size.
It makes definitely more sense to add a value to the queue itself (which indeed increases the queue size):
1
2
3
    if(..) {// some condition is true
 q[i].push(5); // this pushes 5 on the queue and adds 1 to the size 
}


shall i use while loop () instead
no, not from requirements above.
@coder777
i am sorry, i update the topic name now, my paradigm is to increase the size of the ith queue by 5 more times, if a condition is true

Eg., if q[1] has a size of 3 and if the condition is true for that queue, its size is to be increased to 8.

i think this code will do

1
2
3
4
if(..) {// some condition is true
for(int j = 0; j < 5; j++)
  q[i].push(0);
} 


Last edited on
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
30
31
32
33
34
35
36
37
#include <vector>
#include <queue>
#include <stdexcept>
#include <algorithm>
#include <iostream>

using queue_type = std::queue<int> ;

queue_type& shortest_queue( std::vector<queue_type>& seq )
{
    if( seq.empty() ) throw std::domain_error( "empty" ) ;
    return *std::min_element( seq.begin(), seq.end(),
                            [] ( const queue_type& a, const queue_type& b )
                            { return a.size() < b.size() ; } ) ;
}

void increase_queue_size( queue_type& q, int value = 0, std::size_t by = 5 )
{
    auto new_size = q.size() + by ;
    while( q.size() < new_size ) q.push(value) ;
}

int main()
{
    std::vector<queue_type> v(8) ;

    for( int i=0 ; i<5 ; ++i ) for( auto& q : v ) q.push(i) ;
    v[2].pop() ; v[6].pop() ;

    for( const auto& q : v ) std::cout << q.size() << ' ' ;
    std::cout << '\n' ;

    increase_queue_size( shortest_queue(v) ) ;

    for( const auto& q : v ) std::cout << q.size() << ' ' ;
    std::cout << '\n' ;
}
i think this code will do
Yes, with this requirements of course.


@JLBorges
It's good to promote C++11, but it seems a bit more than the OP requested.
I don't like the use of auto. It rather obfuscate the type of a variable
@coder777 how about using std::deque instead of std::vector as it has a resize() function, which resizes the size of queue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <deque> 
#include <queue> 
int min_index = 0;
std::deque<std::queue<int> > q
std::size_t size = q.size();
for( i=0; i<size; i++){ 
    if(...) {// some condition is true
  
  q[i].resize(); // increase the size by 5 times
}
if(q[min_index].size() > q[i].size())
        min_index = i; // Now q[min_index] is the shortest queue
} 
q[min_index].push(int)
}


but how to increase the size of ith queue, using the resize() function, as we
dont know size the ith queue.

is it possible to do like this?

1
2
3
4
5
6
7
8
if(...) {// some condition is true

j=q[i].size;
k=j+5
q[i].resize(k)
  
 
}



Is it worth to use deque with resize() function here, or go with vectors as we did above
Last edited on
how about using std::deque instead of std::vector as it has a resize() function, which resizes the size of queue.
well, if you replace the vector with a deque how would that affect the queue?

if you want something like q[i].resize(); you need to replace the inner queue with a deque.

is it possible to do like this?
yes, but make it simplier:q[i].resize(q[i].size() + 5)

if you use a function like JLBorges showed above you dont need to change the nature of the queue:
1
2
3
4
5
6
7
8
9
10
11
void increase_queue_size( std::queue<int>& q, int value = 0, std::size_t by = 5 )
{
    size_t new_size = q.size() + by ;
    while( q.size() < new_size ) q.push(value) ;
}
...
    if(...) {// some condition is true
  
  increase_queue_size(q[i]); // increase the size by 5 times
}
...


it's up to you what do you like better
Last edited on
@coder777
So shall i use vector of deque's to do the same operation?

Something like the code given below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <vector> 
#include <deque> 
int min_index = 0;
std::vector<std::deque<int> > q
std::size_t size = q.size();
for( i=0; i<size; i++){ 
   if(...) {// some condition is true
  q[i].resize(q[i].size() + 5)
}
if(q[min_index].size() > q[i].size())
        min_index = i; // Now q[min_index] is the shortest queue
} 
q[min_index].push(int)
}


and if want to do an heterogenous container, say it takes two data types (int, string)

shall i use the boost library to deal with the heterogenous container

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <boost/variant.hpp>
#include <vector> 
#include <deque> 
int min_index = 0;
std::vector<std::deque<boost::variant<int, std::string> > q
std::size_t size = q.size();
for( i=0; i<size; i++){ 
   if(...) {// some condition is true
  q[i].resize(q[i].size() + 5) // will push ints and increase the size by 5 times
}
if(q[min_index].size() > q[i].size())
        min_index = i; // Now q[min_index] is the shortest queue
} 
q[min_index].push(string) // will push strings
}
Last edited on
So shall i use vector of deque's to do the same operation?
It's up to you. deque has push_front/pop_frong and push_back/pop_back instead of just push/pop like queue. So q[min_index].push is not longer valid. You have to decide which push/pop combination

shall i use the boost library to deal with the heterogenous container
boost::variant stores only one value (type) at a time. Either string or int but not both.
So this isn't exactly a ' heterogenous container'.
It has a function which() if you need to decide what data type is used

If you want to keep both you need a struct or class.
@coder777 Thanks alot for your interaction.
Last edited on
Topic archived. No new replies allowed.