I have a vector<T> input
from which I want to get n randomly selected elements via the std::sample
algorithm from STL C++17 (http://en.cppreference.com/w/cpp/algorithm/sample). The code works fine in case results
is of type vector<T>
.
Code example 1 (no pointers returned)
auto getSamples(unsigned int noSamples, const vector<T> &input)
{
vector<T> results;
std::mt19937 twisterEngine;
std::sample(input.begin(), input.end(), std::back_inserter(results),
noSamples, twisterEngine);
return results;
}
However, I am looking not for values/copies of the elements stored in input
but I would like to get pointers to the n sampled elements. Are there any tips how I can get pointer returned by vector<T*> results
using only standard c++ code (e.g. not using boost library, etc.)? How do I need to adjust following code to get it done?
Code example 2 (intention to get pointers returned)
auto getSamples(unsigned int noSamples, const vector<T> &input)
{
vector<T*> results;
std::mt19937 twisterEngine;
std::sample(input.begin(), input.end(), std::back_inserter(results),
noSamples, twisterEngine);
return results;
}