BEEP-8 Helper Lib 1.0.0
Loading...
Searching...
No Matches
array.h File Reference

Header file for the b8array class. More...

#include <b8/assert.h>
#include <cstddef>
#include <stdio.h>
#include <stdlib.h>
#include <trace.h>

Go to the source code of this file.

Classes

class  b8array< T, Capacity >
 A fixed-size array with custom behavior for element initialization. More...
 

Enumerations

enum  ArrayOptions::InitializationOption { ArrayOptions::Fill , ArrayOptions::NoFill }
 Enum for specifying array initialization options. More...
 

Detailed Description

Header file for the b8array class.

This header defines a template class b8array that represents a fixed-size array with custom behavior for element initialization. The class supports basic functionalities such as adding elements, accessing elements, and iterating over the array. Additionally, it provides options for initializing the array with default-constructed elements.

Example Usage

Basic Usage with Built-in Type (int)

#include "b8array.h"
b8array<int, 5> arr; // Creates an array with capacity of 5.
arr.push_back(1); // Add elements to the array.
arr.push_back(2);
for (int x : arr) { // Iterate through elements.
std::cout << x << std::endl;
}
A fixed-size array with custom behavior for element initialization.
Definition array.h:111
void push_back(const T &x_)
Add an element to the end of the array.
Definition array.h:305

Basic Usage with Custom Class

Suppose we have a custom class MyClass defined as follows:

class MyClass {
int data;
public:
MyClass(int value) : data(value) {}
int getValue() const { return data; }
};

We can use the b8array class with MyClass in a similar way:

#include "b8array.h"
b8array<MyClass, 3> arr; // Creates an array with capacity of 3.
arr.push_back(MyClass(1)); // Add MyClass objects to the array.
arr.push_back(MyClass(2));
for (const MyClass& obj : arr) { // Iterate through elements.
std::cout << obj.getValue() << std::endl;
}

Using Initialization Option

#include "b8array.h"
// Creates an array with capacity of 5 and fills it with default-constructed MyClass objects.
b8array<MyClass, 5> arr(ArrayOptions::Fill);

Element Access and Removal

#include "b8array.h"
b8array<MyClass, 5> arr(ArrayOptions::Fill);
arr.push_back(MyClass(1));
arr.push_back(MyClass(2));
const MyClass& firstElement = arr.at(0); // Access element by index.
arr.erase(arr.begin()); // Remove the first element.

Copying Arrays

#include "b8array.h"
arr1.push_back(MyClass(1));
arr1.push_back(MyClass(2));
b8array<MyClass, 5> arr2 = arr1; // Copy contents of arr1 into arr2.

Enumeration Type Documentation

◆ InitializationOption

Enum for specifying array initialization options.

Enumerator
Fill 

Fill the array with default-constructed elements.

NoFill 

Do not fill the array.