Use std::vector in normal C++ code; write your own vector as a learning exercise, not as the default container.
std::vector provides the standard properties of a dynamic array: constant-time random access, amortized constant-time insertion/removal at the end, and contiguous storage. A replacement has to get allocation, construction/destruction, copying/moving, iterator invalidation, exception safety, allocator behavior, and performance details right before it is even a credible substitute.
For ordinary use, improve the way you use std::vector instead of replacing it:
#include <vector>
std::vector<int> values;
values.reserve(1000); // if you know the expected size
for (int i = 0; i < 1000; ++i) {
values.push_back(i);
}
reserve() is the standard way to avoid repeated reallocations when the expected size is known.
Implementing a small Vector<T> is a useful exercise. Keep it in test or learning code, compare its behavior against std::vector, and use the exercise to understand why the standard container is the default choice rather than to produce a better general-purpose container.
Dev Price
· 0 rep
· 16 hours ago