Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push String in Stack?

Tags:

c++

stack

stl

I am using C++ and i want to push strings in stack like push int in stacks.

For Example

3."stackoverflow"
2."is"
1."Best"
0."site"

at every index of stack I want to push a string. How can I do this?

like image 782
Mudassar Avatar asked Nov 23 '25 09:11

Mudassar


2 Answers

Using STL, for example:

#include <stack>

std::stack<std::string> s;
s.push("A");
s.push("B");
s.push("C");
s.push("D");

Check the STL reference for more information.

like image 110
Ton van den Heuvel Avatar answered Nov 24 '25 21:11

Ton van den Heuvel


Totally agree with Ton van den Heuvel, however you said

"at every index of stack I want to push a string"

What do you mean "at every index"? You should know that once the strings are in the stack, you can only access the top string and there is no access by index in a stack. If that's what you need, use std::vector instead.

like image 43
Armen Tsirunyan Avatar answered Nov 24 '25 23:11

Armen Tsirunyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!