loop each queue

Pages: 12
@coder777
if i want the pop the element in an another function (which does not have this for loop), and i want the pop the elements of all the queues(not from 2nd to last queue) how can i do that,

for example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
std::vector<std::queue<int> > q
void enqueue(){
int min_index = 1;
std::size_t size = q.size();
for( i=2; 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)
}

void dequeue(){

//q.pop operation , access all the queues in the loop of queues

}


how can i access the loop of queues in another function, willq[i].pop(int); access all the queues in the enqueue function and does the pop operation?
Last edited on
@coder777 Is the question understandable?
@coder777
1
2
3
4
5
6
void dequeue(){ 
std::size_t size = q.size();
 for( i=0; i<size; i++)
{
 q[i].pop(int)
 }


will this one does the required the pop operation for all the queues in a vector of queues?
will this one does the required the pop operation for all the queues in a vector of queues?
Somehow, it does if q is now a global variable.

You need to decide from which end you want to pop the data (pop_front/back).
The same applies to the push function (push_front/back).

enqueue -> dequeue
either
push_front -> pop_back
or
push_back -> pop_front
@coder777
just another small doubt, you said q is a global variable, so do i need to declare again std::size_t size = q.size(); inside the dequeue() function also?
Last edited on
do i need to declare again std::size_t size = q.size(); inside the dequeue() function also?
Yes, because you need the size at the moment you call dequeue()/enqueue(), while q needs to maintain its content all the time.
Topic archived. No new replies allowed.
Pages: 12