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

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

    Try this:

    template
    struct myStack : std::stack {
        typedef std::stack Stack;
        using Stack::stack;
        using Stack::c;                   
    };
    
    int main() 
    {
        myStack> s;
        s.push(1);
        s.push(2);
    
        std::deque::iterator it = s.c.begin();
        while (it != s.c.end())
            std::cout << ' ' << *it++;
    }
    

    Here we expose underlying container of std::stack as member c.

提交回复
热议问题