1: //stack01.cpp 3: #include <iostream> 4: #include <stack> 5: using namespace std; 7: int main() 8: { 9: cout << "\nThis program illustrates the \"LIFO\" (Last In, " 10: "First Out) behavior of a simple\nstack of characters, " 11: "as well as its default constructor, its copy constructor," 12: "\nand the stack push(), pop(), top(), empty(), and size() " 13: "member functions."; 14: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 16: stack<char> s1; 17: s1.push('A'); 18: s1.push('B'); 19: s1.push('C'); 20: s1.push('D'); 21: cout << "\nThe stack s1 contains " << s1.size() << " values."; 23: stack<char> s2(s1); 24: s2.push('E'); 25: cout << "\nThe stack s2 is created as a copy of s1, after which " 26: "another value is added,\nso its size is " << s2.size() << "."; 27: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 29: cout << "\nHere the values of s1, in \"LIFO\" order:\n"; 30: //This is the proper way to access the elements of a stack: 31: while(!s1.empty()) 32: { 33: cout << "Popping: "; 34: cout << s1.top() << "\n"; 35: s1.pop(); 36: } 37: cout << "Press Enter to continue ... "; cin.ignore(80, '\n'); 39: cout << "\nHere the values of s2, in \"LIFO\" order:\n"; 40: while(!s2.empty()) 41: { 42: cout << "Popping: "; 43: cout << s2.top() << "\n"; 44: s2.pop(); 45: } 46: cout << "Press Enter to continue ... "; cin.ignore(80, '\n'); 47: }