简体   繁体   English

堆栈中的 peek() 和 top() 函数有什么区别?

[英]What is the difference between the peek() and top() function in the stack?

我正在使用堆栈并使用 peek 和 top 函数,但我无法得到它们之间的任何区别。

The difference between them is that std::stack::top() is in the standard and that std::stack::peek() is not.它们之间的区别在于std::stack::top()在标准中,而std::stack::peek()不在标准中。

Maybe you are using some custom stack type that derives from std::stack somehow that adds peek() as a member function?也许您正在使用一些从std::stack派生的自定义堆栈类型以某种方式添加peek()作为成员函数? In that case I would expect both functions to behave exactly the same.在这种情况下,我希望两个函数的行为完全相同。

peek() & top() functions are same but peek() is unavailable in std::stack . peek()top()函数相同,但peek()std::stack不可用。 You can check it here- http://www.cplusplus.com/reference/stack/stack/ .你可以在这里查看 - http://www.cplusplus.com/reference/stack/stack/ Actually, peek() and top() both take O(1) time without any other queries.实际上, peek()top()都需要O(1)时间而没有任何其他查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM