Boost logo

Boost :

From: Joaquin M López Muñoz (joaquinlopezmunoz_at_[hidden])
Date: 2024-09-30 17:24:48


El 30/09/2024 a las 18:20, Andrzej Krzemienski via Boost escribió:
> Hi Everyone,
> I would like to check if something like the following would be a useful
> addition to Boost.Core, or if something like this maybe exists:
>
> I have a class like
> ```
> struct X
> {
> std::string str;
> explicit X(const std::string& s) : str(s) {}
> };
> ```
>
> I need to transform a vector of strings into a vector of X through invoking
> the constructor of X:
>
> ```
> std::vector<std::string> strings = {"cat", "dog", "emu", "fox"};
> std::vector<X> xs;
>
> std::transform(strings.begin(), strings.end(),
> std::back_inserter(xs),[](auto const& s) {
> return X(s);
> }));
> ```
>
> It works, but a shorter version would be to introduce a component that is
> equivalent to the lambda:

You can use boost::value_factory:

```
std::vector<std::string> strings = {"cat", "dog", "emu", "fox"};
std::vector<X> xs;

std::transform(
 Â  strings.begin(), strings.end(),
 Â  std::back_inserter(xs), boost::value_factory<X>());
```

Joaquin M Lopez Munoz


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk