When dealing with a const vector, the following doesn't work:
const std::vector<std::string> v;
v.push_back("test"); // error: v cannot be modified
Instead, you have to initialize the vector on the same line where it is constructed. However, even with this restriction, boost::make_transform_iterator
makes it easy to do something with another vector's elements before pushing them into v. In this example, convert
is a unary function that returns a transformed version of an input element:
auto beg = boost::make_transform_iterator(args.begin(), convert);
auto end = boost::make_transform_iterator(args.end(), convert);
const std::vector<const char*> vc { beg, end };
I've taken a look at the functions available in <iterator>
and don't see an equivalent. Is it simply missing or is there a reason why the standard library doesn't have it?
For C++11 there's always the lambda inplace initialization trick:
or
See it Live On Coliru
That's said, I'd prefer the Boost Range adaptors: (also Live On Coliru)