Home > Software design >  What is the difference between the peek() and top() function in the stack?
What is the difference between the peek() and top() function in the stack?

Time:10-19

I am using the stack and using peek and and top functions but I can not get any difference between them.

CodePudding user response:

The difference between them is that std::stack::top() is in the standard and that std::stack::peek() is not.

Maybe you are using some custom stack type that derives from std::stack somehow that adds peek() as a member function? In that case I would expect both functions to behave exactly the same.

CodePudding user response:

peek() & top() functions are same but peek() is unavailable in std::stack. You can check it here- http://www.cplusplus.com/reference/stack/stack/ . Actually, peek() and top() both take O(1) time without any other queries.

  • Related