The Stack Library

Published on: May 20, 2011

In my last tutorial, I introduced stacks the long way. In this tutorial, I use the C++ Stack Library and show you a much shorter way to do stacks. The last tutorial showed you in depth what some of the built in functions of the stack library does and this tutorial covers all the functions included in the C++ Stack Library.

This is a simple tutorial so you will not need to know arrays or structs. But you should know them anyway since they are of great use. View my last two tutorials dealing with Stacks for a better background on stacks and how the inners of stacks in C++ works.

If you have any questions about the program or anything in general relating to beginner C++ Programming, feel free to ask.

Remember to checkout the Codesnip below to review the C++ code yourself! Feel free to copy the code, but I ask that you please provide credit if you leave the code unchanged when you use it.

Codesnip:

//Programmer: Nazmus
//Program:  Using the Stack Library
//Website:  EasyProgramming.net

#include<iostream>

#include<stack>
#include<string>

using namespace std;

int main()
{

 stack<int> name;
 int n;
 char quest;

 cout << "Do you want to enter data (Y/N)? ";
 cin >> quest;

 while(quest=='Y' || quest=='y')
 {

  cout << "Please enter name: ";
  cin >> n;

  name.push(n);

  cout << "Do you want to enter data (Y/N)? ";
  cin >> quest;
 }

 cout << "\nThe total size of your stack is "<< 
  name.size() << endl;

 cout << "\nThese are the values of your stack:\n\n";

 while(!name.empty())
 {

  cout << name.top() << endl;

  name.pop();
 }

 
 system("pause");
}



Comments: