Array

Description

This template class represents a dynamic, single dimensional array (similar to std::vector container).

You can access individual elements of array by their zero based index, using array subscript operator - [].

template <typename T>
class Array
{
public:		
	int      Size() const;
	int      Capacity() const;
	bool	 Empty() const;

	T&       Front();
	const T& Front() const;
	T&       Back();
	const T& Back() const;

	T*       Begin();
	const T* Begin() const;
	T*       End();
	const T* End() const;

	void	 Clear();

	Array();
	explicit Array( int size, const T& value = T());		
	Array( const Array& rhs );
	Array<T>& operator = ( const Array& rhs );
	~Array();

	bool operator == ( const Array& rhs ) const;
	bool operator != ( const Array& rhs ) const;

	void Reset( unsigned int newSize = 0, const T& value = T());
	void Resize( unsigned newSize, const T& c = T());
	void Reserve( unsigned int newCapacity );

	T& operator[] ( int k );
	const T& operator[] ( int k ) const;

	void PushBack( const T& value );
	void PopBack();

	void Insert( int index, const T& value );
	void Insert( T* target, const T& value );
	void Insert( const T* begin, const T* end );
	void Insert( T* target, const T* begin, const T* end );

	T* Erase(T* target);
	T* Erase(T* begin, T* end);

	void Swap( Array& rhs );

#ifdef HAS_CPP0X

	Array( Array&& rhs );
	Array<T>& operator = ( Array&& rhs );
	void PushBack( T&& value );

#endif
};

Methods:

  • int Size() - returns number of elements stored in array.
  • bool Empty() - returns true when array contains no elements.
  • T& Front() - returns reference to first element in array (array must not be empty).
  • T& Back() - returns reference to last element in array (array must not be empty).
  • T* Begin() - returns pointer to first element in array.
  • T* End() - returns pointer to first after last element in array.
  • void PushBack( const T& value ) - inserts value as last element of array
  • PopBack() - removes last element of array (array must not be empty).

Following operators can be used on Array class:

  • []( int index ) - returns reference to array element distinguished by zero based index.
  • == - for comparison with other Array object.
  • = - to copy Array content into other object.