Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,602 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 949 people online right now. Registration is fast and FREE... Join Now!




STL: Stack

 
Reply to this topicStart new topic

> STL: Stack, A simple "how to"

gabehabe
Group Icon



post 14 Jul, 2008 - 05:43 AM
Post #1


STL: Stacks
Mega-simple, but still good to know!

What is a stack?
A stack is basically like a pile of objects. Much like a queue, only a stack serves a "first come, last served" rule.
Basically, when we call pop() to remove a value from our stack, it will remove the last addition, as opposed to the first. In this demonstration, I will use a simple deck of cards to show you how a stack can be implemented.

Including the stuff that needs to be included
Very simple, just #include <stack> and we'll be using std::stack; But, as I want to show you how this works, I'm gonna #include <iostream> too smile.gif
cpp
#include <iostream>
#include <stack>

using namespace std;

int main ()
{

Creating a stack
Very simple, similar syntax to all other STL stuff:
cpp
stack <string> cards; /* Simple enough to create a stack */

Adding values to the stack
Again, this is really simple. All we need to do is use the push() function to add values to our stack:
cpp
    cards.push("King of Hearts"); /* push() will add a value, think of queues */
cards.push("King of Clubs"); /* adding some cards to the deck */
cards.push("King of Diamonds");
cards.push("King of Spades");

Accessing the size of the stack
This can be done with a quick and simple call to the size() function, like so:
cpp
    cout << "There are " << cards.size () << " cards in the deck" << endl;

Access to the values stored in a stack
Think of a deck of cards. You are only allowed to take the top card, right? The same applies to a stack, we can only access the top() value stored in a stack:
cpp
cout << "The card on the top of the deck is " << cards.top() << endl;
This should output "The card on the top of the deck is King of Spades" since this was the last card added to the deck.

Removing a value from the stack
The only value that we can remove from the stack is the top one. (This is the only value that we actually have access to) To remove a value from a stack, we need to make a call to the pop() function, like so:
cpp
cards.pop();

Finally, I'm going to output the top card and the size again, so that we can now see that "King of Spades" has been removed from the stack:
cpp
    cout << "The top card is now " << cards.top() << endl;
cout << cards.size();

And don't forget to close off main! }

And there we have it. That's all there is to using a stack!
Also, you can refer to

Here is the entire code which I used in this tutorial:
cpp
#include <iostream>
#include <stack>

using namespace std;

int main ()
{
stack <string> cards; /* Simple enough to create a stack */
cards.push("King of Hearts"); /* push() will add a value, think of queues */
cards.push("King of Clubs"); /* adding some cards to the deck */
cards.push("King of Diamonds");
cards.push("King of Spades");
cout << "There are " << cards.size () << " cards in the deck" << endl;
cout << "The card on the top of the deck is " << cards.top() << endl;
/* Will output King of Spades, since this was the last one to be added */
cards.pop();
cout << "The top card is now " << cards.top() << endl;
cout << cards.size();

cin.get ();
return EXIT_SUCCESS;
}
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 11/23/08 02:11AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month