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

后端 未结 8 1716
忘掉有多难
忘掉有多难 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:28

    ostream & operator<<(ostream & os, stack my_stack) //function header
    {
        while(!my_stack.empty()) //body
        {
            os << my_stack.top() << " ";
            my_stack.pop();
        }
        return os; // end of function
    }
    
    
    /*
    using this simple overloaded operator function, you're able to print out all the components of a stack by doing a simple cout << your_stack_name;*/
    

提交回复
热议问题