STL: StacksMega-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 includedVery 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
cpp
#include <iostream>
#include <stack>
using namespace std;
int main ()
{
Creating a stackVery simple, similar syntax to all other STL stuff:
cpp
stack <string> cards; /* Simple enough to create a stack */
Adding values to the stackAgain, 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 stackThis 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 stackThink 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 stackThe 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
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;
}