sincerely Singaporean

If you have not done so, read this full tutorial on how to use SGEXTN to build an application.

SGLStack

see header file

(no source file, everything inside header)

template ‹typename T› class SGLStack;

part of SGEXTN module SG_Containers

stack for any type of data

detailed description

list of all including inherited members

implementation details

preprocessor file inclusion directive: #include ‹SGLStack.h›

CMake target for BuildLah: SGEXTN::SG_Containers

see this link for more information about BuildLah

parent class: (none)

children classes: (none)

instance member functions

SGLStack();

[[nodiscard]] int length() const;

void pop();

void push(const T& x);

void reserve(int newMemoryLength);

[[nodiscard]] const T& top() const;

Detailed Description

SGLStack provides a template based stack for any data type. SGLStack is preferred over SGLVector when only stack functionality is needed as it provides more readable code. This is a template based class with no separate source file. This class is a SGEXTN container. Copy constructor, copy assignment, move constructor, move assignment, and destructor work as expected. A deep copy is performed whenever this class is copied, and the new instance will not be linked to the old instance in any way. It is assumed that the contents placed into this SGEXTN container can be copied and moved. This means that their copy constructor copy assignment, move constructor, move assignment, and destructor work as expected. If this is not the case or if you want the container to store references or constant references, store pointers instead.

Implementation Details

SGLStack internally stores a buffer in the form of a C array. When the buffer is full, it resizes to double its previous size.

SGLStack();

Creates an empty SGLStack.

[[nodiscard]] int length() const;

Returns the length of the SGLStack, that is the number of elements currently stored inside it.

void pop();

Removes the top element of the SGLStack.

This does not return the removed element. To access the removed element, use SGLStack::top before removal.

This runs in truly constant time complexity.

Removing from an empty SGLStack will crash.

void push(const T& x);

Appends x to the top of the SGLStack.

This has a amortised constant time complexity. To make it run in truly constant time, use SGLStack::reserve to pre allocate as much memory as you need.

void reserve(int newMemoryLength);

Pre allocates sufficient memory to store newMemoryLength elements in the SGLStack.

If there is already sufficient memory for newMemoryLength elements, this is ignored.

[[nodiscard]] const T& top() const;

Returns the topmost element of the SGLStack.

Using this when the SGLStack is empty will crash.

©2025 05524F.sg (Singapore)

contact 05524F / report a bug / make a suggestion

about 05524F SINGAPORE values

list of 05524F projects