Click on main.cpp to get source.
// File: C++Examples/Stacks/main.cpp
//
// This is the C++ version of the C example
//
// ../CExamples/OO_pgm_in_C/
//
// One would not normally write a stack in C++
// this way, but use the STL stack adaptor.
//
// The C++ version is distinguished from
// the C version in that the main program is
// not generic. For that purpose, one could have a
// function
//
// stack* make_stack();
//
// in array_stack.cpp that is loaded at link time.
// Vice-versa, the C version could store a pointer
// to the new_stack() in struct stack, so that
// both list_stacks and array_stacks can be used
// in main.
//
// Reference:
// http://users.cs.duke.edu/~elk27/bibliography/96/EKM96.pdf
#include <iostream>
#include "array_stack.h"
// for STL stack adaptor below
#include <stack>
#include <vector>
#include <iterator>
// Using the STL stack adaptor
typedef std::stack< int, std::vector<int> > stl_stack;
std::ostream& operator<<(std::ostream& os, const stl_stack& S)
// print stack contents by popping a copy of the stack
{stl_stack& S2 = *(new stl_stack(S));
os << "STL stack contents: ";
while(!S2.empty())
{os << S2.top() << " "; S2.pop();}
os << " bottom";
delete &S2;
return os;
}//operator<<
// second solution: make a subclass to access
// protected member "Container c" in stl_stack (C++ standard, Section 23.2.3.3)
class sub_stack : public stl_stack
{
friend std::ostream& operator<<(std::ostream& os, const sub_stack& Sub);
};//class substack
std::ostream& operator<<(std::ostream& os, const sub_stack& Sub)
// print stack contents by traversing container
{std::ostream_iterator<int> out(os, " ");
os << "Sub stack contents: ";
std::copy( Sub.stl_stack::c.begin() // resolve scope of member c to base class
, Sub.c.end() // use inheritance of member c of base claaaaaaaaass
, out);
os << "bottom";
return os;
}//operator<<
// can't use namespace std because conflict with "stack" name
int main(void)
// Test the stack
{int i;
stack& st = *(new array_stack());
std::cout << st << std::endl;
for (i=1; i < 6; i++)
{ std::cout << "Pushing " << i << std::endl;
st.push(i);
std::cout << st << std::endl;
}
for (i=0; i < 3; i++)
{ std::cout << "Popping " << st.pop() << std::endl;
std::cout << st << std::endl;
}
// use of STL stack container adapter
stl_stack& stl_st = *(new stl_stack());
std::cout << std::endl << stl_st << std::endl;
for (i=1; i < 6; i++)
{ std::cout << "Pushing " << i << std::endl;
stl_st.push(i);
std::cout << stl_st << std::endl;
}
for (i=0; i < 3; i++)
{ std::cout << "Popping " << stl_st.top() << std::endl;
stl_st.pop();
std::cout << stl_st << std::endl;
}
// use subclass of STL stack container adapter
sub_stack& sub_st = *(new sub_stack());
std::cout << std::endl << sub_st << std::endl;
for (i=1; i < 6; i++)
{ std::cout << "Pushing " << i << std::endl;
sub_st.push(i);
std::cout << sub_st << std::endl;
}
for (i=0; i < 3; i++)
{ std::cout << "Popping " << sub_st.top() << std::endl;
sub_st.pop();
std::cout << sub_st << std::endl;
}
delete &st;
delete &stl_st; delete &sub_st;
return 0;
}