STL Stack of C Plus Plus Programming Language

STL Stack of C Plus Plus Programming Language

Instructor-svgAl-Mamun Sarkar
Apr 03 , 2020

STL Stack of C Plus Plus Programming Language. The following code shows how to use stack in C++ programming language.

 

Code:

#include < iostream >
#include < stack >

using namespace std;

int main() {
    std::stack<int> test_stack;
    int sum(0);

    for (int i = 1; i <= 10; i++) {
        test_stack.push(i);
    }


    while (!test_stack.empty()) {
        cout << test_stack.top() << endl;
        sum += test_stack.top();
        test_stack.pop();
    }

    std::cout << "total: " << sum << '\n';

    return 0;
}

 

Output:

10
9
8
7
6
5
4
3
2
1
total: 55

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram