CMagic  0.5.0
Portable C library of utilities and data structures
Public Types | Public Member Functions | Static Public Member Functions | List of all members
cmagic::vector< T > Class Template Reference

A sequence container representing array that can change in size. More...

#include <vector.hpp>

Public Types

using value_type = T
 Type of vector elements. More...
 
using size_type = size_t
 Type used to measure element size and position in a vector. More...
 

Public Member Functions

 vector ()
 Constructs an empty vector with standard memory allocation. More...
 
size_type size () const
 Returns the number of elements in the vector. More...
 
bool empty () const
 Returns whether the vector is empty (i.e. whether its size is 0). More...
 
value_typeoperator[] (size_type pos)
 Returns a reference to the element at position pos in the vector container. More...
 
const value_typeoperator[] (size_type pos) const
 Returns a reference to the element at position pos in the vector container. More...
 
bool push_back (const value_type &val)
 Add element at the end. More...
 
bool push_back (value_type &&val)
 Add element at the end. More...
 
template<typename... Args>
bool emplace_back (Args &&... args)
 Construct and insert element at the end. More...
 
void pop_back ()
 Delete last element. More...
 
void clear ()
 Removes all elements from the vector (which are destroyed), leaving the container with a size of 0. More...
 
value_typebegin ()
 Return iterator to beginning. More...
 
const value_typebegin () const
 Return iterator to beginning. More...
 
value_typeend ()
 Return iterator to end. More...
 
const value_typeend () const
 Return iterator to end. More...
 
 operator bool () const
 Checks if the vector is properly initialized. More...
 

Static Public Member Functions

static vector custom_allocation_vector ()
 Constructs an empty vector using custom CMagic memory allocation from memory.h. More...
 

Detailed Description

template<typename T>
class cmagic::vector< T >

A sequence container representing array that can change in size.

The content forms contiguous storage location. Size can change dynamically, with storage being handled automatically by the container. Reallocations if needed happen only at logarithmically growing intervals of size so that the insertion of individual elements at the end of the vector can be provided with amortized constant time complexity.

Member Typedef Documentation

◆ size_type

template<typename T >
using cmagic::vector< T >::size_type = size_t

Type used to measure element size and position in a vector.

◆ value_type

template<typename T >
using cmagic::vector< T >::value_type = T

Type of vector elements.

Constructor & Destructor Documentation

◆ vector()

template<typename T >
cmagic::vector< T >::vector ( )
inline

Constructs an empty vector with standard memory allocation.

Returns
a new empty vector

Member Function Documentation

◆ begin() [1/2]

template<typename T >
value_type* cmagic::vector< T >::begin ( )
inline

Return iterator to beginning.

Returns an iterator pointing to the first element in the vector. If the container is empty, the returned iterator value shall not be dereferenced.

Returns
an iterator to the beginning of the sequence container

◆ begin() [2/2]

template<typename T >
const value_type* cmagic::vector< T >::begin ( ) const
inline

Return iterator to beginning.

Returns an iterator pointing to the first element in the vector. If the container is empty, the returned iterator value shall not be dereferenced.

Returns
an iterator to the beginning of the sequence container

◆ clear()

template<typename T >
void cmagic::vector< T >::clear ( )
inline

Removes all elements from the vector (which are destroyed), leaving the container with a size of 0.

◆ custom_allocation_vector()

template<typename T >
static vector cmagic::vector< T >::custom_allocation_vector ( )
inlinestatic

Constructs an empty vector using custom CMagic memory allocation from memory.h.

Returns
a new empty vector

◆ emplace_back()

template<typename T >
template<typename... Args>
bool cmagic::vector< T >::emplace_back ( Args &&...  args)
inline

Construct and insert element at the end.

Inserts a new element at the end of the vector, right after its current last element. This new element is constructed in place using args as the arguments for its constructor. This effectively increases the container size by one, which causes an automatic reallocation of the allocated storage space if the new vector size surpasses the current vector capacity.

Parameters
argsarguments forwarded to construct the new element

◆ empty()

template<typename T >
bool cmagic::vector< T >::empty ( ) const
inline

Returns whether the vector is empty (i.e. whether its size is 0).

This function does not modify the container in any way. To clear the content of a vector, see vector::clear.

Returns
true if the container size is 0, false otherwise

◆ end() [1/2]

template<typename T >
value_type* cmagic::vector< T >::end ( )
inline

Return iterator to end.

Returns an iterator referring to the past-the-end element in the vector container. The past-the-end element is the theoretical element that would follow the last element in the vector. It does not point to any element, and thus shall not be dereferenced.

Returns
an iterator to the element past the end of the sequence

◆ end() [2/2]

template<typename T >
const value_type* cmagic::vector< T >::end ( ) const
inline

Return iterator to end.

Returns an iterator referring to the past-the-end element in the vector container. The past-the-end element is the theoretical element that would follow the last element in the vector. It does not point to any element, and thus shall not be dereferenced.

Returns
an iterator to the element past the end of the sequence

◆ operator bool()

template<typename T >
cmagic::vector< T >::operator bool ( ) const
inline

Checks if the vector is properly initialized.

Example usage:

if (vec) {
vec.push_back(123);
} else {
std::cerr << "Vector allocation failed!\n";
}
Returns
true if vector is initialized, false if vector allocation has failed and no operation should be made on it

◆ operator[]() [1/2]

template<typename T>
value_type& cmagic::vector< T >::operator[] ( size_type  pos)
inline

Returns a reference to the element at position pos in the vector container.

Warning
Never call this function with an argument n that is out of range, since this causes undefined behavior.
Parameters
posPosition of an element in the container. Notice that the first element has a position of 0 (not 1).
Returns
the element at the specified position in the vector

◆ operator[]() [2/2]

template<typename T>
const value_type& cmagic::vector< T >::operator[] ( size_type  pos) const
inline

Returns a reference to the element at position pos in the vector container.

Warning
Never call this function with an argument n that is out of range, since this causes undefined behavior.
Parameters
posPosition of an element in the container. Notice that the first element has a position of 0 (not 1).
Returns
the element at the specified position in the vector

◆ pop_back()

template<typename T>
void cmagic::vector< T >::pop_back ( )
inline

Delete last element.

Warning
Do not use this function if the vector is empty

Removes the last element in the vector, effectively reducing the container size by one.

◆ push_back() [1/2]

template<typename T>
bool cmagic::vector< T >::push_back ( const value_type val)
inline

Add element at the end.

Adds a new element at the end of the vector, after its current last element. The content of val is copied (or moved) to the new element. This effectively increases the container size by one, which causes an automatic reallocation of the allocated storage space if the new vector size surpasses the current vector capacity.

Parameters
valvalue to be copied (or moved) to the new element

◆ push_back() [2/2]

template<typename T>
bool cmagic::vector< T >::push_back ( value_type &&  val)
inline

Add element at the end.

Adds a new element at the end of the vector, after its current last element. The content of val is copied (or moved) to the new element. This effectively increases the container size by one, which causes an automatic reallocation of the allocated storage space if the new vector size surpasses the current vector capacity.

Parameters
valvalue to be copied (or moved) to the new element

◆ size()

template<typename T>
size_type cmagic::vector< T >::size ( ) const
inline

Returns the number of elements in the vector.

This is the number of actual objects held in the vector, which is not necessarily equal to its storage capacity.

Returns
number of elements in the vector

The documentation for this class was generated from the following file: