Skip to content

C++ Array

PucklaJ edited this page Feb 29, 2024 · 2 revisions

Array - C++

Back to Home

dro::Array<T>

An Array takes ownership over some arbitrary C array and frees its memory in the destructor. The memory of the C array NEEDS to be allocated by malloc, calloc, realloc or reallocarray!

New

static Array<T> New(size_t size);

Allocates memory and creates a new array with it.

  • size Number of elements of the new array

Constructor

Array() noexcept;
Array(T *data, size_t size, bool delete_data = true) noexcept;
Array(Array<T> &&rhs) noexcept;
Array(const Array<T> &rhs) = delete;
Array<T> &operator=(Array<T> &&rhs) noexcept;

When constructing a new array it takes ownership over some C array. Only move semantics are allowed.

  • data An C array allocated by malloc, calloc, realloc or reallocarray
  • size The number of elements inside data
  • delete_data Wether to free data in the destructor

Indexing

T &operator[](size_t index);
const T &operator[](size_t index) const;
  • index Which element to return

Comparing

bool operator==(const char *str2) const noexcept;
bool operator==(const std::string &str2) const noexcept;
bool operator==(const Array<T> &rhs) const noexcept;

Equality against a string can be checked only if the array itself can be considered a string.

data

 T *data() noexcept;
  const T *data() const noexcept;

Returns the raw pointer to the underlying C array.

size

size_t size() const noexcept;

Returns the number of elements in the array.

empty

bool empty() const noexcept;

Returns wether no elements are in the array.

str

std::string str() const noexcept;

Converts the array to a string. Works only with uint8_t, int8_t and char.

Iterating

Iterator begin() noexcept;
Iterator end() noexcept;
ConstIterator begin() const noexcept;
ConstIterator end() const noexcept;

The Array can be iterated in a foreach loop.

dro::String

This is a null terminated string of char.

Constructor

String(char *str, bool delete_data = true) noexcept;

Create a String from a C string.

  • str The C string. If delete_data is true it must be allocated by either malloc, calloc, realloc or reallocarray.
  • delete_data Wether to delete str in the destructor

dro::SizedString

This is a sized string of char. Which means that it does not necessarily end with a null terminator.

Clone this wiki locally