将字符串压入堆栈?
我正在使用 C++,我想将字符串推送到堆栈中,就像推送 int
一样。
例如,
3."stackoverflow"
2."is"
1."Best"
0."site"
在堆栈的每个索引处,我想推送一个字符串。我该怎么做?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
例如,使用 STL:
查看 STL 参考以获取更多信息。
Using STL, for example:
Check the STL reference for more information.
完全同意 Ton van den Heuvel,不管你怎么说
“在每个索引处”是什么意思?您应该知道,一旦字符串进入堆栈,您只能访问顶部字符串,并且无法通过堆栈中的索引进行访问。如果这就是您需要的,使用 std::vector 代替。
Totally agree with Ton van den Heuvel, however you said
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.
我做到了 。我之前看到了如何将字符串转换为字符数组,并将其与我现在正在学习的内容混合在一起。堆栈。
输出为
tihom
。它的功能很简单。它将字符串转换为字符数组,然后逐个字符地推送字符串。为了弹出,我们使用相同的循环,并且它使用 LIFO 原理弹出。
I did it . I saw earlier how to convert string to character array and mixed it to what I am studying now ie. stacks.
And output was
tihom
.It's function is simple. It converts string to character array and then pushes the string character by character. To pop out we use same loop and it pops out using LIFO principle.