#include <iostream>
#include <memory>
template<typename T>
class Pector {
private:
int m_size;
int m_capacity;
std::unique_ptr<T[]>m_ptr;
public:
Pector(int capacity) : m_capacity{ capacity }, m_size{capacity}, m_ptr{std::make_unique<T[]>(capacity)} {}
void print() {
for (int i{}; i < m_size; i++) {
std::cout << *(m_ptr.get() + i) << '\n';
}
}
// Function that change a value at an index
T& operator[](int index) {
if (index < m_size) {
return *(m_ptr.get() + index);
}
}
// Delete (sets it to 0 to let the possibility to add an element without recreating an entire array) the last element of the Pector
void pop() {
if (m_size > 0) {
*(m_ptr.get() + (m_size - 1)) = 0;
m_size--;
}
}
void push(T value) {
if(m_size >= m_capacity) {
m_size++;
m_capacity++;
std::unique_ptr<T[]>tempPtr{ std::make_unique<T[]>(m_capacity) };
for (int i{}; i < m_capacity; i++) {
if (i == m_capacity - 1) {
*(tempPtr.get() + i) = value;
}
else {
*(tempPtr.get() + i) = *(m_ptr.get() + i);
}
}
// m_ptr now points to tempPtr
m_ptr.swap(tempPtr);
tempPtr.reset();
}
else {
m_size++;
m_ptr[m_size - 1] = value;
}
}
};
what do you guys think of it?
If I would continue to working on my custom vector, raw pointer shouold probably be the right thing, because yea smart pointers have their limitations