Stack Implementation

How to implement stack<string,int>

hi @Gurjot you can have a stack of the type pair<string, int> like stack<pair<string, int>>
or you can make a class/struct like this

struct s{
    string str;
    int n;
};

and then make a stack of the type s, like stack<s>

how to self implement the stack<int,string> using the templated class

@Gurjot take a vector of type pair and insert/remove pairs from it instead of making 2 different vectors

@Gurjot this implementation will also work, just provide proper arguments everywhere


Please make the necessary changes in this code, so that I could get the concept clearly

@Gurjot https://ide.codingblocks.com/s/323946 i fixed the arguments so its not giving errors now, please see

@Gurjot here’s the second code https://ide.codingblocks.com/s/323949
if you want to use a class/struct you will have to first create an object of that class and then insert it, if you just have 2 elements then it is easier to use pair in that case

1 Like

Still not outputting the data of different data types

@Gurjot how will it output without initialisation and cout statements??

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.