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:
const auto vc = [&]{
std::vector<const char*> tmp(v.size());
std::transform(v.begin(), v.end(), tmp.begin(), convert);
return tmp;
}();
or
const auto vc = [&]{
std::vector<const char*> tmp;
tmp.reserve(v.size());
std::transform(v.begin(), v.end(), back_inserter(tmp), convert);
return tmp;
}();
See it Live On Coliru
That's said, I'd prefer the Boost Range adaptors: (also Live On Coliru)
const auto vc = boost::copy_range<std::vector<const char*> >(v | transformed(convert));
#include <algorithm>
#include <vector>
#include <iterator>
#include <string>
#include <functional>
#include <iostream>
int main()
{
const std::vector</* const */ std::string> v { "test", "goodbye" };
auto convert = std::mem_fn(&std::string::c_str);
const auto vc = [&]{
std::vector<const char*> tmp;
tmp.reserve(v.size());
std::transform(v.begin(), v.end(), back_inserter(tmp), convert);
return tmp;
}();
for (auto cc : vc)
std::cout << cc << "\n";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With