stack libraries

Discussion in 'C++' started by Mario, Jun 6, 2004.

  1. Mario

    Mario Guest

    Hi
    i´m searching for a good stack solution also, but is there no stack librarie
    in the standart libraries like the string class?
    and if there is a stack class how to use
    thanks a lot
    mario
    Mario, Jun 6, 2004
    #1
    1. Advertising

  2. Mario

    Phlip Guest

    Mario wrote:

    > i´m searching for a good stack solution also, but is there no stack

    librarie
    > in the standart libraries like the string class?
    > and if there is a stack class how to use


    #include <list>

    class Stack {
    public:
    void push(int);
    int pop();

    private:
    std::list<int> m_ints;
    };

    Notice your client code should not know there is a std::list<> in there.
    Clients should only push and pop the stack. Implementation of push and pop
    are up to you, but they will probably immediately delegate to std::list<>
    methods.

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
    Phlip, Jun 6, 2004
    #2
    1. Advertising

  3. Mario

    Petec Guest

    Mario wrote:
    > Hi
    > i´m searching for a good stack solution also, but is there no stack
    > librarie in the standart libraries like the string class?
    > and if there is a stack class how to use
    > thanks a lot
    > mario


    The standard library does have a stack:

    #include <stack>
    std::stack<int> stack;
    stack.push(123);
    int v = stack.top(); stack.pop();

    Replace "int" with whatever type you want it to store.

    - Pete
    Petec, Jun 6, 2004
    #3
  4. Mario

    Petec Guest

    Phlip wrote:
    > Mario wrote:
    >
    >> i´m searching for a good stack solution also, but is there no stack
    >> librarie in the standart libraries like the string class?
    >> and if there is a stack class how to use

    >
    > #include <list>
    >
    > class Stack {
    > public:
    > void push(int);
    > int pop();
    >
    > private:
    > std::list<int> m_ints;
    > };
    >
    > Notice your client code should not know there is a std::list<> in
    > there. Clients should only push and pop the stack. Implementation of
    > push and pop are up to you, but they will probably immediately
    > delegate to std::list<> methods.


    Aside from the fact that the standard library already has a stack class,
    wouldn't a std::deque<> be a better choice than std::list<> ? The standard
    one defaults to the deque.

    - Pete
    Petec, Jun 6, 2004
    #4
  5. Mario

    David Harmon Guest

    On Sun, 6 Jun 2004 16:12:02 +0200 in comp.lang.c++, "Mario"
    <> wrote,
    >Hi
    >i´m searching for a good stack solution also, but is there no stack librarie
    >in the standart libraries like the string class?


    What is wrong with std::stack for your purpose?
    David Harmon, Jun 6, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Surinder Singh
    Replies:
    1
    Views:
    1,172
    Richard Bos
    Dec 20, 2007
  2. Casey Hawthorne
    Replies:
    3
    Views:
    1,061
    Flash Gordon
    Nov 1, 2009
  3. Debajit Adhikary
    Replies:
    36
    Views:
    2,230
    Andre Kaufmann
    Feb 10, 2011
  4. Sam Roberts
    Replies:
    1
    Views:
    208
    Yukihiro Matsumoto
    Feb 11, 2005
  5. Kenneth McDonald

    Why stack overflow with such a small stack?

    Kenneth McDonald, Aug 30, 2007, in forum: Ruby
    Replies:
    7
    Views:
    242
    Kenneth McDonald
    Sep 1, 2007
Loading...

Share This Page