### 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
 ``1234567891011121314151617`` ``````#include #include int min_index = 0; std::vector > q std::size_t size = q.size(); for( i=0; i q[i].size()) min_index = i; // Now q[min_index] is the shortest queue } q[min_index].push(int) }``````

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:
 ``12`` ``````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
 ``123456789101112131415`` ``````#include #include int min_index = 0; std::vector > q std::size_t size = q.size(); for( i=0; i 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):
 ``123`` `````` 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

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

Last edited on
 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````#include #include #include #include #include using queue_type = std::queue ; queue_type& shortest_queue( std::vector& 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 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.

 ``123456789101112131415`` ``````#include #include int min_index = 0; std::deque > q std::size_t size = q.size(); for( i=0; i 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?

 ``12345678`` ``````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:
 ``1234567891011`` ``````void increase_queue_size( std::queue& 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
 ``1234567891011121314`` ``````#include #include int min_index = 0; std::vector > q std::size_t size = q.size(); for( i=0; i 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

 ``123456789101112131415`` ``````#include #include #include int min_index = 0; std::vector > q std::size_t size = q.size(); for( i=0; i 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.