A use case emerged when wanting to do a contitional copy (1. doable with copy_if
) but from a container of values to a container of pointers to those values (2. doable with transform
).
With the available tools I can't do it in less than two steps :
#include <vector>
#include <algorithm>
using namespace std;
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
vector<ha*> pv; // temporary vector
// 1.
transform(v.begin(), v.end(), back_inserter(pv),
[](ha &arg) { return &arg; });
// 2.
copy_if(pv.begin(), pv.end(), back_inserter(ph),
[](ha *parg) { return parg->i < 2; }); // 2.
return 0;
}
Ofcourse we could call remove_if
on pv
and eliminate the need for a temporary, better yet though, it's not difficult to implement (for unary operations) something like this :
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;
}
// example call
transform_if(v.begin(), v.end(), back_inserter(ph),
[](ha &arg) { return &arg; }, // 1.
[](ha &arg) { return arg.i < 2; });// 2.
- Is there a more elegant workaround with the available C++ standard library tools ?
- Is there a reason why
transform_if
does not exist in the library? Is the combination of the existing tools a sufficient workaround and/or considered performance wise well behaved ?
After just finding this question again after some time, and devising a whole slew of potentially useful generic iterator adaptors I realized that the original question required NOTHING more than
std::reference_wrapper
.Use it instead of a pointer, and you're good:
Live On Coliru
Prints
This is just an answer to question 1 "Is there a more elegant workaround with the available C++ standard library tools ?".
If you can use c++17 then you can use
std::optional
for a simpler solution using only C++ standard library functionality. The idea is to returnstd::nullopt
in case there is no mapping:See live on Coliru
Note that I just implemented Rust's approach in C++ here.