Is there a way to construct a new std::vector with uninitialized (non-zero) values or even prettier through a generator (similar to std::generate_n()) constructor argument that produces the desired non-standard (non-constant) values without first initializing all elements to zero? This because I want a (random) mockup-creation api to be as locality-efficient as possible, thereby only writing container elements once. Wouldn't it be neat to have a generator-constructor for the std::vector (and possibly others)?! Why hasn't C++ already added this to the Standard?
The following constructor-like C function illustrates the write-once-behaviour I seek for the custom-initialize construction of std::vector:
// Allocate-and-Generate a random int array of length \p n.
int * gen_rand(size_t n)
{
int *v = malloc(n); // allocate only
for (size_t i=0; i<n; i++) {
v[i] = rand(); // first write
}
}
I believe it boils down to the behaviour of the STL Allocator used, since it is responsible for writing the initial zeros (or not).
If we use the std::vector constructor with iterators we first have to allocate and write the random values somewhere else, even worse than using push_back().
You can precede the use of the generator with a call to
vector::reserve. This will have exactly the same behavior as the C code you show. You will still need to use aback_insert_iteratoras the size of thevectorwill still be zero.