how to print out all elements in a std::stack or std::queue conveniently

后端 未结 8 1719
忘掉有多难
忘掉有多难 2020-12-14 20:46

If I do not to want to create a new container in order to do so?

8条回答
  •  遥遥无期
    2020-12-14 21:42

    You can't iterate through a stack or a queue. In fact, SGI's documentation says this (it's about stack, but it's the same reason for queue):

    This restriction is the only reason for stack to exist at all. Note that any Front Insertion Sequence or Back Insertion Sequence can be used as a stack; in the case of vector, for example, the stack operations are the member functions back, push_back, and pop_back. The only reason to use the container adaptor stack instead is to make it clear that you are performing only stack operations, and no other operations.

    So, if you really want to do this, you'll have to empty the stack (or queue):

    std::stack s;
    // ...
    while(!s.empty())
    {
        Whatever w = s.top();
        std::cout << w;
        s.pop();
    }
    

提交回复
热议问题