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

时间:2021-07-12 17:40:06

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

我需要执行for循环,直到队列为空我的代码

queue<string> q;
for(int i=0;i<q.size(),i++)
{
     // some operation goes here
     // some datas are added to queue
}

5 个解决方案

#1


8  

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

    // TODO: do something with str.

    q.pop();
}

#2


2  

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

它与最佳答案的代码相同,但使用for循环。它对我来说看起来更干净。

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

    // TODO: do something with str.
}

#3


0  

Its better to use a while loop as:

最好使用while循环:

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:

但是如果你在声明队列后立即执行此操作,则不会进入循环,因为队列在创建时将为空。在这种情况下,您可以使用do-while循环:

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

#4


0  

while ( ! q.empty() )
{
}

#5


0  

Yes its possible.

是的可能。

int size=q.size();
for(int i=0;i<size;i++){
    std::cout<<"\nCell - "<< q.front();
    q.pop();
}

But people mostly avoid using for loop because, every time size of queue will be checked against loop counter, where in the middle of n/2 elemets pop up iteration will end up ubruptly as size will become n/2 and i is also n/2. Example mentioned below.

但人们大多避免使用for循环,因为每次队列的大小都会针对循环计数器进行检查,其中在n / 2个元素的中间弹出迭代将会突然结束,因为大小将变为n / 2而且我也是n / 2。下面提到的例子。

for(int i=0;i<q.size();i++){
    std::cout<<"\nCell - "<< q.front();
    std::cout<<"\tSize: - "<< q.size()<<" I value:"<<i;
    q.pop();
}

#1


8  

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

    // TODO: do something with str.

    q.pop();
}

#2


2  

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

它与最佳答案的代码相同,但使用for循环。它对我来说看起来更干净。

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

    // TODO: do something with str.
}

#3


0  

Its better to use a while loop as:

最好使用while循环:

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:

但是如果你在声明队列后立即执行此操作,则不会进入循环,因为队列在创建时将为空。在这种情况下,您可以使用do-while循环:

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

#4


0  

while ( ! q.empty() )
{
}

#5


0  

Yes its possible.

是的可能。

int size=q.size();
for(int i=0;i<size;i++){
    std::cout<<"\nCell - "<< q.front();
    q.pop();
}

But people mostly avoid using for loop because, every time size of queue will be checked against loop counter, where in the middle of n/2 elemets pop up iteration will end up ubruptly as size will become n/2 and i is also n/2. Example mentioned below.

但人们大多避免使用for循环,因为每次队列的大小都会针对循环计数器进行检查,其中在n / 2个元素的中间弹出迭代将会突然结束,因为大小将变为n / 2而且我也是n / 2。下面提到的例子。

for(int i=0;i<q.size();i++){
    std::cout<<"\nCell - "<< q.front();
    std::cout<<"\tSize: - "<< q.size()<<" I value:"<<i;
    q.pop();
}