views:

52

answers:

3

hello, i need to execute an for loop till the queue is empty my code

queue<string> q;
for(int i=0;i<q.size(),i++)
{
     // some operation goes here
     // some datas are added to queue
}
+2  A: 
while (!q.empty())
{
    std::string str = q.front();

    // TODO: do something with str.

    q.pop();
}
asveikau
A: 

Its better to use a while loop as:

while (!q.empty()) {
 // do operations.
}

But if you do this immediately after declaring the queue you'll not get in the loop as the queue will be empty on creation. In that case you can use a do-while loop as:

queue<string> q;
do {
// enqueue and dequeue here.
}while (!q.empty());
codaddict
A: 
while ( ! q.empty() )
{
}
vulkanino