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
}
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
}
while (!q.empty())
{
std::string str = q.front();
// TODO: do something with str.
q.pop();
}
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());