vector<T>.cpp — Custom Implementation
A from-scratch implementation of std::vector focusing on Manual Memory Management, RAII principles, and Strong Exception Guarantees.
Geometric Growth
Uses a 2x growth factor to achieve amortized O(1) push_back performance, minimizing reallocation frequency.
Move Semantics
Optimized for efficient transfer of resources via Move semantics, significantly reducing expensive deep copies.
Custom Allocator
Utilizes ::operator new to separate memory allocation from object construction.
The Implementation
Performance Considerations
One of the critical sections of a vector is the Reallocation Logic. When the capacity is reached, we must allocate a new chunk of memory and move the existing elements. By using std::move_if_noexcept, I ensure that if the move constructor might throw, we fallback to a copy to maintain the Strong Exception Guarantee (the original vector remains unchanged if reallocation fails).