stack libraries

M

Mario

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
 
P

Phlip

Mario said:
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.
 
P

Petec

Mario said:
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
 
P

Petec

Phlip said:
#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
 
D

David Harmon

On Sun, 6 Jun 2004 16:12:02 +0200 in comp.lang.c++, "Mario"
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top