tags:

views:

158

answers:

3

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?

+11  A: 

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 sgi STL reference for more information.

Ton van den Heuvel
+6  A: 

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.

Armen Tsirunyan
If cplusplus.com is correct (http://www.cplusplus.com/reference/stl/stack/), the default underlying type of `std::stack` is `std::deque`, not `std::vector`. +1 anyway, but you should correct that.
Matteo Italia
@Matteo: sorry, got mixed up, vector was the default for priority_queue... editing
Armen Tsirunyan
A: 

There is some misunderstanding here. I want to push strings in stack. just like to push as integers. push("Hello"); push("world");

give me the code plz for this.

mudasar