This C++ program illustrates usage of a adaptor called stack. An adapter is a data type that adapts a container to provide specific interface. Stack provides facility to push elements into it and pop them.
Here is the source code of the C++ program illustrates usage of stack. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to Illustrate Stack Adaptor
*/
#include <iostream>
#include <stack>
int main()
{
std::stack< char > s;
// Pushing elements into stack
for (char c = 'a'; c <= 'f'; c++)
{
s.push(c);
}
while (!s.empty())
{
std::cout << "Top element of stack \'" << s.top()
<< "\'" << std::endl;
s.pop();
}
std::cout << "Stack is empty!" << std::endl;
}
$ ./a.out Top element of stack 'f' Top element of stack 'e' Top element of stack 'd' Top element of stack 'c' Top element of stack 'b' Top element of stack 'a' Stack is empty!
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Next Steps:
- Get Free Certificate of Merit in C++ Programming
- Participate in C++ Programming Certification Contest
- Become a Top Ranker in C++ Programming
- Take C++ Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Practice Programming MCQs
- Buy Computer Science Books
- Practice Computer Science MCQs
- Apply for C++ Internship
- Apply for Computer Science Internship