#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template <
class InputIterator, class OutputIterator,
class UnaryOperator, class Pred
>
OutputIterator transform_if(InputIterator first1, InputIterator last1,
OutputIterator result, UnaryOperator op, Pred pred)
{
while (first1 != last1)
{
if (pred(*first1)) {
*result = op(*first1);
++result;
}
++first1;
}
return result;
}
struct ha {
int i;
explicit ha(int a) : i(a) {}
};
int main()
{
vector<ha> v{ ha{1}, ha{7}, ha{1} }; // initial vector
// GOAL : make a vector of pointers to elements with i < 2
vector<ha*> ph; // target vector
// example call
transform_if(v.begin(), v.end(), back_inserter(ph),
[](ha &arg) { return &arg; }, // 1.
[](ha &arg) { return arg.i < 2; });// 2.
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnRlbXBsYXRlIDwKICAgIGNsYXNzIElucHV0SXRlcmF0b3IsIGNsYXNzIE91dHB1dEl0ZXJhdG9yLCAKICAgIGNsYXNzIFVuYXJ5T3BlcmF0b3IsIGNsYXNzIFByZWQKPgpPdXRwdXRJdGVyYXRvciB0cmFuc2Zvcm1faWYoSW5wdXRJdGVyYXRvciBmaXJzdDEsIElucHV0SXRlcmF0b3IgbGFzdDEsCiAgICAgICAgICAgICAgICAgICAgICAgICAgICBPdXRwdXRJdGVyYXRvciByZXN1bHQsIFVuYXJ5T3BlcmF0b3Igb3AsIFByZWQgcHJlZCkKewogICAgd2hpbGUgKGZpcnN0MSAhPSBsYXN0MSkgCiAgICB7CiAgICAgICAgaWYgKHByZWQoKmZpcnN0MSkpIHsKICAgICAgICAgICAgKnJlc3VsdCA9IG9wKCpmaXJzdDEpOwogICAgICAgICAgICArK3Jlc3VsdDsKICAgICAgICB9CiAgICAgICAgKytmaXJzdDE7CiAgICB9CiAgICByZXR1cm4gcmVzdWx0Owp9CgpzdHJ1Y3QgaGEgeyAKCWludCBpOwog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