STL Stack of C Plus Plus Programming Language

Author: Al-mamun Sarkar Date: 2020-04-03 20:03:59

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