Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to execute an for loop till the queue is emptyin c++

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
}
like image 764
raja Avatar asked Oct 07 '10 05:10

raja


2 Answers

while (!q.empty())
{
    std::string str = q.front();

    // TODO: do something with str.

    q.pop();
}
like image 167
asveikau Avatar answered Sep 21 '22 02:09

asveikau


It's the same code as the best answer, but using for loop. It looks cleaner for me.

for (; !q.empty(); q.pop())
{
    auto& str = q.front();

    // TODO: do something with str.
}
like image 27
Virus_7 Avatar answered Sep 20 '22 02:09

Virus_7