shared_ptr<vector<int> > result(new vector<int>() );
You create a new empty vector.
transform(numbers->begin(), numbers->end(), result->begin(), [this](int x){ return factor * x; });
Since result empty, result->begin() returns the value of the final iterator. std::transform copies the input sequence, applies the lambda transform, and writes the converted result to the output iterator.
Since the vector is empty, there is nothing to write. You run through the end of an empty array, which leads to undefined behavior and memory corruption.
In this case, just predefine the output array, since you know what its size is, in advance:
shared_ptr<vector<int> > result(new vector<int>(numbers->size()) );
Now this will create an output array of the correct size, begin() will return the iterator to the beginning of the array, and std::transform() happily scratch the array.
If you really want to avoid the additional overhead of initializing a new array, just to overwrite it, you can use reserve() to pre-allocate the final size of the array, and then use std::back_insert_iterator for the output iterator, instead of passing to begin() .
source share