Request Short Link
C++ 98
C++ 11
C++ 14
C++ 17
C++ 20
C++ 23
C++ 2c
for-loops as while-loops
array subscription
Show all implicit casts
Show all template parameters of a CallExpr
Use libc++
Transform std::initializer_list
Show noexcept internals
Show padding information
Show coroutine transformation
Show C++ to C transformation
Show object lifetime
Default
15
18
20
22
26
More
GitHub
Patreon
Issues
About
Policies
Examples
C++ Insights @ YouTube
Settings
Version
None
×
Made by
Andreas Fertig
Powered by
Flask
and
CodeMirror
Source:
#include <iostream> #include <stdexcept> #include <initializer_list> template <typename T> class Vector { private: int capacity_; int size_; T* ptr_; public: Vector() :capacity_{ 0 }, size_{ 0 }, ptr_{ nullptr } {} Vector(int size) : capacity_{ size }, ptr_{ new T[size] }, size_{ size } {} Vector(int size, T data) : Vector(size) { for (int i{ 0 }; i < size; ++i) ptr_[i] = data; } Vector(std::initializer_list<T> list) { clear(); for (const T& elem : list) push_back(elem); } //Destructor ~Vector() { clear(); } //Copy constructor Vector(const Vector& v) { if (this == &v) return; capacity_ = v.capacity_; size_ = v.size_; ptr_ = new T[v.size_]; for (int i{ 0 }; i < v.size_; ++i) ptr_[i] = v.ptr_[i]; } //Copy assignment operator Vector<T>& operator=(const Vector<T>& v) { if (this != &v) { delete[] ptr_; ptr_ = nullptr; capacity_ = v.capacity_; size_ = v.size_; ptr_ = new T[capacity_]; for (int i{ 0 }; i < v.size_; ++i) ptr_[i] = v.ptr_[i]; } return *this; } T& operator[](int i) { if (i >= 0 && i < size_) return ptr_[i]; else throw std::out_of_range("Index out of bounds."); } T& operator[](int i) const { if (i >= 0 && i < size_) return ptr_[i]; else throw std::out_of_range("Index out of bounds."); } void reserve(int size) { if (size_ < capacity_) return; if (ptr_ == nullptr) { size_ = 0; capacity_ = 0; } T* bufferNew = new T[size]; unsigned int l_size = std::min(capacity_, size); for (int i{ 0 }; i < l_size; ++i) { bufferNew[i] = ptr_[i]; } if (ptr_ != nullptr) delete[] ptr_; ptr_ = bufferNew; capacity_ = size; } void clear() { if (ptr_ != nullptr) delete[] ptr_; ptr_ = nullptr; size_ = 0; capacity_ = 0; } int size() const { return size_; } int capacity() { return capacity_; } void push_back(const T& elem) { if (size_ >= capacity_) { reserve(capacity_ + 5); // Double the capacity } ptr_[size_++] = elem; } void pop_back() { --size_; } T front() { if (size_ > 0) return ptr_[0]; else throw std::out_of_range("Index out of bounds."); } T back() { if (size_ > 0) return ptr_[size_ - 1]; else throw std::out_of_range("Index out of bounds."); } T* getRawPointer() { return ptr_; } }; template <typename T> Vector<T> operator+(const Vector<T>& v1, const Vector<T>& v2) { if (v1.size() != v2.size()) throw std::logic_error("Vector lengths must be equal."); Vector<T> result; for (int i{ 0 }; i < v1.size(); ++i) result.push_back(v1[i] + v2[i]); return result; } int main() { Vector<double> x; x = Vector<double>({ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 }); return 0; }
Insight:
Console: