我有兴趣 将此 Python 代码 移植到 C++。作为端口的一部分,我正在使用来自 std::stack
<stack>
标头的 —。如何确定某个字符是否包含在 stack<char>
中?例如:
std::stack<char> myStack
if (!('y' is included in myStack)) // I know that this is wrong
{
}
原文由 george mano 发布,翻译遵循 CC BY-SA 4.0 许可协议
我有兴趣 将此 Python 代码 移植到 C++。作为端口的一部分,我正在使用来自 std::stack
<stack>
标头的 —。如何确定某个字符是否包含在 stack<char>
中?例如:
std::stack<char> myStack
if (!('y' is included in myStack)) // I know that this is wrong
{
}
原文由 george mano 发布,翻译遵循 CC BY-SA 4.0 许可协议
3 回答2k 阅读✓ 已解决
2 回答3.9k 阅读✓ 已解决
2 回答3.2k 阅读✓ 已解决
1 回答3.2k 阅读✓ 已解决
1 回答2.7k 阅读✓ 已解决
3 回答3.4k 阅读
1 回答1.6k 阅读✓ 已解决
C++
stack
不支持随机访问,因此无法直接使用stack
检查是否包含元素。但是,您可以制作堆栈的副本,然后连续pop
离开该堆栈,直到找到该元素。或者,如果您确实需要搜索
stack
,您可以考虑改用deque
,它确实支持随机访问。例如,您可以在 --- 上使用deque
find
算法来搜索元素:If you need to frequently search of the
stack
, consider keeping a parallelstd::set
along with thestack
that stores the same elements as thestack
。这样,您可以只使用set::find
来(有效地)检查元素是否存在。希望这可以帮助!