Vector added
This commit is contained in:
38
random-access-containers/vector.hpp
Normal file
38
random-access-containers/vector.hpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
#include <stdexcept>
|
||||
|
||||
template<class T>
|
||||
class Vector
|
||||
{
|
||||
protected:
|
||||
T * data;
|
||||
std::size_t size;
|
||||
|
||||
public:
|
||||
void Resize(std::size_t const newSize)
|
||||
{
|
||||
void * result = std::realloc(data, newSize);
|
||||
if(result == nullptr)
|
||||
{
|
||||
throw std::runtime_error("Cannot allocate the requested size of memory.");
|
||||
}
|
||||
|
||||
size = newSize;
|
||||
data = reinterpret_cast<T *>(result);
|
||||
}
|
||||
|
||||
T & operator[](std::size_t const index)
|
||||
{
|
||||
if(index >= size)
|
||||
{
|
||||
throw std::out_of_range("Index is greater or equal to size.");
|
||||
}
|
||||
return data[index];
|
||||
}
|
||||
|
||||
std::size_t GetSize() const
|
||||
{
|
||||
return size;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user