Skip to main content

C++ STL Stack Container Adaptor – std::stack

In this tutorial you will learn about STL stack container adaptor in C++ i.e. std::stack and all functions which it provides.

std::stack is a container adaptor. We know that container adaptors are not containers. They provide specific interfaces. Elements manipulated in container adaptors by encapsulated functions of specific classes.

Stack operates in Last in First out (LIFO) type of arrangement. Always elements will be inserted and also deleted at same side of the stack.

Working with direct operations on stack/queue and other container adaptors, will so much useful in competitive programming. It saves time and also encapsulated functions of object implemented in best complexity way. When program size too large, instead of writing entire code if we use direct functions from library that gives unambiguity while working.

C++ STL Stack Container Adaptor – std::stack

To work with stl container, we first need to include stack header file.

#include<stack>

The functions associated with stack are:

push(element): Inserting elements into stack is called “push” operation.

pop(element): Removing elements into stack is called “pop” operation.

top(element): Displays the top element of the stack.

size(element): Returns the size of the stack

empty(): This is Boolean operation which returns whether the stack is empty or not.

Program to show the above functions on stack:

#include<iostream>
#include<stack>

using namespace std;

int main()
{
        stack <int> stk; // declearing stack

        for (int i=0; i<5; i++){
                // pushing elements into stack
                stk.push(i);
        }

        cout << "size of the stack is ";
        cout << stk.size() << endl;
        
        cout << "top of the stack is ";
        cout << stk.top() << endl;
        
        // to show all elements we should pop each time.
        // Since we can only access top of the stack.
        cout << "elements of the stack are " << endl ;
        for (int i=0; i<5; i++){
                cout << stk.top() << " ";
                stk.pop(); // popping element after showing
        }
        cout << endl;
        
        if (stk.empty() == 1) {
                cout << "finally stack is empty " << endl;
        }
        else {
            cout << "stack is not empty " << endl;
        }       
        
        return 0;
}

Output

size of the stack is 5
top of the stack is 4
elements of the stack are
4 3 2 1 0
finally stack is empty

One other operations is:

swap(): Swap function swaps the elements in one stack to other.

#include <iostream>
#include <stack>

using namespace std;

int main(){
        stack <int> stk1, stk2;

        for (int i=1; i<6; i++){
                stk1.push(i+10);
        }
        
        cout << "elements 11, 12, 13, 14, 15 pushed into stack 1" << endl;
        for (int i=1; i<6; i++){
                stk2.push(i*10);
        }
        
        cout << "elements 10, 20, 30, 40, 50 pushed into stack 2" << endl;
        
        cout << "doing swapping operation..... " << endl;
        stk1.swap(stk2);
        cout << "after swapping " << endl;
        
        cout << "elements of stack 1 are " ;
        for (int i=0; i<5; i++){
                cout << stk1.top() << " ";
                stk1.pop();
        }

        cout << endl;
        cout << "elements of stack 2 are " ;
        for (int i=0; i<5; i++){
                cout << stk2.top() << " ";
                stk2.pop();
        }
        
        return 0;
}

Output

elements 11, 12, 13, 14, 15 pushed into stack 1
elements 10, 20, 30, 40, 50 pushed into stack 2
doing swapping operation…..
after swapping
elements of stack 1 are 50 40 30 20 10
elements of stack 2 are 15 14 13 12 11

Comment below if you have any queries related to above tutorial for stl stack or std::stack.

The post C++ STL Stack Container Adaptor – std::stack appeared first on The Crazy Programmer.



from The Crazy Programmer https://www.thecrazyprogrammer.com/2017/09/stl-stack.html

Comments

Popular posts from this blog

Rail Fence Cipher Program in C and C++[Encryption & Decryption]

Here you will get rail fence cipher program in C and C++ for encryption and decryption. It is a kind of transposition cipher which is also known as zigzag cipher. Below is an example. Here Key = 3. For encryption we write the message diagonally in zigzag form in a matrix having total rows = key and total columns = message length. Then read the matrix row wise horizontally to get encrypted message. Rail Fence Cipher Program in C #include<stdio.h> #include<string.h> void encryptMsg(char msg[], int key){ int msgLen = strlen(msg), i, j, k = -1, row = 0, col = 0; char railMatrix[key][msgLen]; for(i = 0; i < key; ++i) for(j = 0; j < msgLen; ++j) railMatrix[i][j] = '\n'; for(i = 0; i < msgLen; ++i){ railMatrix[row][col++] = msg[i]; if(row == 0 || row == key-1) k= k * (-1); row = row + k; } printf("\nEncrypted Message: "); for(i = 0; i < key; ++i) f...

Experimental: Reducing the size of .NET Core applications with Mono's Linker

The .NET team has built a linker to reduce the size of .NET Core applications. It is built on top of the excellent and battle-tested mono linker . The Xamarin tools also use this linker so it makes sense to try it out and perhaps use it everywhere! "In trivial cases, the linker can reduce the size of applications by 50%. The size wins may be more favorable or more moderate for larger applications. The linker removes code in your application and dependent libraries that are not reached by any code paths. It is effectively an application-specific dead code analysis ." - Using the .NET IL Linker I recently updated a 15 year old .NET 1.1 application to cross-platform .NET Core 2.0 so I thought I'd try this experimental linker on it and see the results. The linker is a tool one can use to only ship the minimal possible IL code and metadata that a set of programs might require to run as opposed to the full libraries. It is used by the various Xamarin products to extract...

Accessibility Insights for the Web and Windows makes accessibility even easier

I recently stumbled upon https://accessibilityinsights.io . There's both a Chrome/ Edge extension and a Windows app, both designed to make it easier to find and fix accessibility issues in your websites and apps. The GitHub for the Accessibility Insights extension for the web is at https://github.com/Microsoft/accessibility-insights-web and they have three trains you can get on: Canary (released continuously) Insider (on feature completion) Production (after validation in Insider) It builds on top of the Deque Axe core engine with a really fresh UI. The "FastPass" found these issues with my podcast site in seconds - which kind of makes me feel bad, but at least I know what's wrong! However, the most impressive visualization in my opinion was the Tab Stop test! See below how it draws clear numbered line segments as you Tab from element. This is a brilliant way to understand exactly how someone without a mouse would move through your site. I can easily s...