开发者

Where are the implementation of stack functions?

开发者 https://www.devze.com 2023-04-01 20:17 出处:网络
I am learning data structures and algorithms. My teacher told me the following: push(): push inserts an element in the stack

I am learning data structures and algorithms. My teacher told me the following:

push(): push inserts an element in the stack

pop(): pop deletes the last inserted element from the sta开发者_如何学运维ck

size(): Returns the number of elements in the stack

isempty(): Returns a boolean indicating if the stack is empty

top(): returns the top element of the stack, without removing it; If stack is empty an error is returned.

Where are the implementations of these functions? Are these built-in functions of C++?


Your teacher was explaining the functions available for the generic stack data structure. Those functions aren't implemented anywhere in particular because your teacher was not talking about any specific stack. Your teacher wasn't even talking about C++. Right now you're just learning about what a stack is: they're abstract data structures implementable in any language. They're last-in-first-out containers. Later in your course, you'll learn about trees, queues, heaps, lists, and all sorts of other abstract data structures. In a later lesson, your teacher will probably demonstrate how to implement the functions listed above. The demonstration might even be in C++.


These are member functions for std::stack, which is a container class (template) in the C++ standard library.

You can find the implementation in the <stack> header file.

[Note that it's empty(), not isempty(), though.]


std::stack - it's in the standard library, which is "part" of c++, although it's not a "built-in function"


If you are trying to figure out how to create your own template classes and functions, see

  • http://www.cplusplus.com/doc/tutorial/templates/

If you want to understand how a stack could be implemented, and what a stack is, read

  • http://en.wikipedia.org/wiki/Stack_%28data_structure%29

If you want to understand more about containers in C++ STL, read these:

  • http://www.cplusplus.com/reference/stl/stack/
  • http://www.cplusplus.com/reference/stl/

Hopefully, these references will help you with asking a more specific question or to request clarification on something you don't yet understand...


To use the stl you would have to include all or part of it in your files, or reference the stl each time you use it.

Here is a link to an implementation of the STL: http://www.sgi.com/tech/stl/download.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号