sincerely Singaporean

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

SGLVector

see header file

(no source file, everything inside header)

template ‹typename T› class SGLVector;

part of SGEXTN module SG_Containers

resizable array for any type of data

detailed description

list of all including inherited members

implementation details

preprocessor file inclusion directive: #include ‹SGLVector.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

SGLVector(int count, const T& defaultValue);

SGLVector(int count);

SGLVector();

void assign(int count, const T& defaultValue);

[[nodiscard]] const T& at(int i) const;

[[nodiscard]] T& at(int i);

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

void fill(const T& defaultValue);

[[nodiscard]] int length() const;

[[nodiscard]] const T* pointerToData(int n) const;

[[nodiscard]] T* pointerToData(int n);

void popBack();

void pushBack(const T& x);

void reserve(int newMemoryLength);

Detailed Description

SGLVector provides a template based vector (resizable array) that works with any data type. 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

SGLDeque internally stores a buffer in the form of a C array. When the buffer is full and more elements need to be appended, the buffer doubles in size.

SGLVector(int count, const T& defaultValue);

Creates a SGLVector of size count with every element set to defaultValue.

Negative count will crash.

SGLVector(int count);

Creates a SGLVector of size count with every element default initialised.

This will not compile if the data in the SGLVector do not have a default constructor (the one taking no arguments).

Negative count will crash.

SGLVector();

Creates an empty SGLVector.

void assign(int count, const T& defaultValue);

Assigns a SGLVector(count, defaultValue) to this SGLVector.

v.assign(count, defaultValue); is identical in functionality to v = SGLVector‹T›(count, defaultValue); Choosing one over the other is purely coding style.

Negative count will crash.

[[nodiscard]] const T& at(int i) const;

Returns a constant reference to element i of the SGLVector.

This will crash for out of bounds i.

[[nodiscard]] T& at(int i);

Returns a reference to element i of the SGLVector.

Since this returns a reference and not a copy, assigning to it directly (using it as a lvalue) would modify the SGLVector.

This will crash for out of bounds i.

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

Returns a constant reference to the last element of the SGLVector.

v.back() has the same effect as v.at(v.length() - 1)

Use SGLVector::atSGLVector::at if the element needs to be modified.

This will crash if the SGLVector is empty.

void fill(const T& defaultValue);

Sets every element in the existing SGLVector to defaultValue without resizing the SGLVector.

If the SGLVector may need to be resized, use SGLVector::assign instead.

[[nodiscard]] int length() const;

Returns the length of the SGLVector, which is the number of elements currently stored in it.

[[nodiscard]] const T* pointerToData(int n) const;

Returns a read only pointer to element n of the SGLVector. This does not dereference the returned pointer.

Using an out of bounds value for n is ok for this function as long as the returned pointer is not dereferenced. Out of bounds values for n may be used intentionally to specify parts of the SGLVector to run SGLSort on.

The returned value may function as an iterator.

Dereferencing the returned pointer when n is out of bounds results in undefined behaviour.

[[nodiscard]] T* pointerToData(int n);

Returns a pointer to element n of the SGLVector. This does not dereference the returned pointer.

Using an out of bounds value for n is ok for this function as long as the returned pointer is not dereferenced. Out of bounds values for n may be used intentionally to specify parts of the SGLVector to run SGLSort on.

The returned value may function as an iterator.

Assigning to the dereferenced value from the returned pointer would modify the SGLArray

Dereferencing the returned pointer when n is out of bounds results in undefined behaviour.

void popBack();

Removes the last element of the SGLVector.

This does not return the removed element. Use SGLVector::back before removal to access the element.

This runs at true constant time complexity.

Removing from empty SGLVector will crash.

void pushBack(const T& x);

Appends x to the SGLVector.

This runs at amortised constant time complexity. To make it truly constant time, pre allocate the memory needed using SGLVector::reserve.

void reserve(int newMemoryLength);

Pre allocates enough memory in the buffer to store newMemoryLength elements in the SGLVector.

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

©2025 05524F.sg (Singapore)

contact 05524F / report a bug / make a suggestion

about 05524F SINGAPORE values

list of 05524F projects