I have n number of queues (depends on the no. of threads ) and what i need is that, i want loop each of queue starting from from the 2nd queue to the nth (end) queue.
how can i loop this?
std::queue<int> queue;
for(queue=queue+1; queue<=n; queue++) // will this idea loop from the 2nd to the nth (end) queue
please check and let me know some ideas how to loop the queues
the idea i need to solve - loop each queue and get the size of each queue and minimum size among the given number of queues.
i tried this logic
std::queue<int> q;
/* fill queue ... */
int min_value = INT_MAX;
std::size_t size = q.size();
for( q=1; q<n; q++){ // given loop of queues
if {
(q.size()<min_value) // q.size() is compared with the min_value (limits MAX)
min_value=q.size(); // store minimum size among the queues.
}
}
i think i succeeded in getting the minimum size among the the queues.
But i dont know whether looping of queues is correct?
It's basically a container where you put in data to one side and get data out from the other side. The data in between is queued.
You cannot use it as an index for a loop.
the idea i need to solve - loop each queue and get the size of each queue and minimum size among the given number of queues.
why do you think you need that?
The link you provided shows how to feed a thread with data using a queue. Each thread itself manage its own queue.
store minimum size among the queues
Either you mean the items of the queue (a queue doesn't support access to it's item other than first/last)
or you mean multiple queues (organized via array, vector ... as I've shown above)
@coder777
i have multiple queues( equal to the number of threads). i have to access the each queue and find its size.
std::queue<int> q[num_threads];
int min_value = INT_MAX;
std::size_t size = q[i].size();
for( i=1; i<num_threads; i++){ //accessing loop of queues
if {
(min_value > q[i].size())
min_value = q[i].size() // q[i].size() is compared with the min_value (limits MAX)
}
}
i think this code will loop through the multiple queuesand store the minimum size among the different queues.
but i need to loop through the multiple queues till last queue (equal to number of threads), but here is it not possible because i<num_threads right? so how to loop till the end queue. will i=num_threads will work?
i think this code will loop through the multiple queues=num of threads and store the minimum size among the different queues.
yes
but i need to loop through the multiple queues till last queue (equal to number of threads), but here is it not possible i<num_threads, so how to loop till the end queue. will i=num_threads will work?
I'd think that you mean num_threads is not constant.
organize your queues in a vector or list: std::vector<std::queue<int> > q_vector;
but one doubt is as i mentioned i<num_threads will it loop till the last queue of the thread or last but before one?
the last queue.
You will always see i<... when iterating over arrays. arrays are 0 based (so is vector) hence you have the valid indexes 0, 1, 2 for an array with the size of 3 (like int a[3]) -> for(int i = 0; i < 3; i++)
and this notation q_vector[num_threads] should be given or just q_vector is enough
q_vector[num_threads] is an array of vectors. You don't want that.
how a size() function can find the number of queues? size() is the function which give the no. of the elements in each queue right?
Ok, you're right it's like so:
1 2 3 4 5
std::size_t size = q_vector.size(); // Note that this is the size of the vector
for( i=1; i< size; i++){ //accessing loop of queues
if {
(min_value > q_vector[i].size()) // Note that this is the size of the queue
min_value = q_vector[i].size()
most STL [compliant] container have functions that are named equally
No it wont, because min_value is the size of the queue which is the smallest, not the index of one, you need to remember the index of the queue when you get the size, add one extra int and set his value on "i" inside if.