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:
//basics/copy_semantics.cpp #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) { std::cout << "\nCopy assignment operator"; 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; } Vector<Vector<double>> createAndInsert() { Vector<Vector<double>> pts; pts.reserve(3); Vector<double> x{ 1.0, 1.0 }; pts.push_back(x); pts.push_back(x + x); pts.push_back(x); return pts; } int main() { Vector<Vector<double>> result = createAndInsert(); return 0; }
Insight:
Console: