Implement Vector class in c++17, for both constant and non-constant objects.
operator= is obliged to call operator= from the i-th object, if possible, and if it is impossible, then construct it in place by calling the copy constructor.
All memory work must be done through the Alloc allocator.
The implementation of the Vector class in C++17 according to your requirements can be seen below. This implementation utilizes the given Alloc allocator for all memory operations, respects both constant and non-constant contexts for operator[], and properly handles assignment and construction of elements through the operator= and the copy constructor. For simplicity, exception safety and iterator support are beyond the scope of this implementation, but should be considered for a more robust and standard-compliant vector class.
cpp
#include <memory>
#include <algorithm>
#include <stdexcept>
#include <new>
template<typename T, typename Alloc = std::allocator<T>>
class Vector {
private:
T* data;
size_t count;
size_t cap;
Alloc allocator;
void destroy_elements() {
for (size_t i = 0; i < count; ++i) {
allocator.destroy(&data[i]);
}
}
public:
Vector() : data(nullptr), count(0), cap(0), allocator(Alloc()) {
}
explicit Vector(unsigned int size) : Vector() {
resize(size);
}
Vector(unsigned int size, const T& defaultValue) : Vector() {
resize(size, defaultValue);
}
Vector(const Vector& another) : Vector() {
resize(another.count);
std::uninitialized_copy(another.data, another.data + another.count, data);
}
Vector& operator=(const Vector& another) {
if (this != &another) {
if (another.size() > capacity()) {
Vector tmp(another);
std::swap(data, tmp.data);
std::swap(count, tmp.count);
std::swap(cap, tmp.cap);
std::swap(allocator, tmp.allocator);
} else {
if (another.size() < size()) {
// Destroy extra elements
for (size_t i = another.size(); i < size(); ++i) {
allocator.destroy(&data[i]);
}
}
// Assign or construct elements in place as necessary
for (size_t i = 0; i < another.size(); ++i) {
if (i < size()) {
data[i] = another.data[i]; // Assignment operator
} else {
allocator.construct(&data[i], another.data[i]); // Copy constructor
}
}
count = another.size();
}
}
return *this;
}
~Vector() {
destroy_elements();
allocator.deallocate(data, cap);
}
T& operator[](unsigned int index) {
if (index >= count) throw std::out_of_range("Index out of range");
return data[index];
}
const T& operator[](unsigned int index) const {
if (index >= count) throw std::out_of_range("Index out of range");
return data[index];
}
void push_back(const T& element) {
if (count == cap) {
resize(capacity() > 0 ? capacity() * 2 : 1);
}
allocator.construct(&data[count], element);
++count;
}
void pop_back() {
if (count > 0) {
--count;
allocator.destroy(&data[count]);
}
}
void resize(size_t newSize) {
resize(newSize, T());
}
void resize(size_t newSize, const T& value) {
if (newSize > cap) {
// Allocate new array
T* newData = allocator.allocate(newSize);
// Move old elements to the new array
for (size_t i = 0; i < count; ++i) {
allocator.construct(&newData[i], std::move(data[i]));
allocator.destroy(&data[i]);
}
// Construct new elements
for (size_t i = count; i < newSize; ++i) {
allocator.construct(&newData[i], value);
}
// Deallocate old array
allocator.deallocate(data, cap);
// Reassign our pointers
data = newData;
cap = newSize;
} else {
for (size_t i = count; i < newSize; ++i) {
allocator.construct(&data[i], value);
}
for (size_t i = newSize; i < count; ++i) {
allocator.destroy(&data[i]);
}
}
count = newSize;
}
size_t size() const {
return count;
}
size_t capacity() const {
return cap;
}
void shrink_to_fit() {
if (count < cap) {
if (count == 0) {
allocator.deallocate(data, cap);
data = nullptr;
cap = 0;
} else {
T* newData = allocator.allocate(count);
for (size_t i = 0; i < count; ++i) {
allocator.construct(&newData[i], std::move(data[i]));
allocator.destroy(&data[i]);
}
allocator.deallocate(data, cap);
data = newData;
cap = count;
}
}
}
};
This Vector class supports basic operations such as push/pop elements, resize, access by index, and ensures objects are constructed and assigned properly. Please note that this implementation does not include all the features and safety checks of std::vector, such as iterator support, exception safety guarantees, or allocator traits support, which would be necessary for a fully-featured and compliant Standard Library container.