-1

I recently started learning about stacks in C++. While looking at an example, I noticed they used the following:

       void showstack(stack <int> s).

I was wondering what the <> did, and how is it different from just using int?

H. Martin
  • 55
  • 7

4 Answers4

4

What you are looking at are template parameters.

Basically, stack is a template, declared (in a simplified manner) like this:

template <class T>
class stack { /*...*/ };

Thus, stack is not a class, you cannot talk about a stack type. It will only become a type once the template parameter is specified; for example: stack<int> is a stack of integers in this case.

Victor
  • 13,914
  • 19
  • 78
  • 147
  • That makes sense, thanks. I could not find anything online about it, but now that i know what they are i will read about it. – H. Martin Oct 12 '19 at 18:27
  • I recommend starting with one of the books in [this list](https://stackoverflow.com/q/388242/1673776). You will get a much better view over the concepts of C++. C++ is the language that can do anything and everything :). Also, if you found my answer useful, please do not forget to mark it as accepted. – Victor Oct 12 '19 at 19:58
1

stack just like many other containers is templated. You need to specify the type of elements you are going to store in the container.

template<
    class T, // this what you specified in your code
    class Container = std::deque<T>
> class stack;

There is no stack compiled in your code until you use one with a type, like you did.

Oblivion
  • 7,176
  • 2
  • 14
  • 33
0

It says that showstack's parameter is a stack of ints.

jkb
  • 2,376
  • 1
  • 9
  • 12
0

The <> are used for templates, which tell a class what kind of data to expect. You could replace int with string and you would have a stack of strings that way.

For more info