Using boost :: lexical_cast with std :: transform

g ++ does not like:

vector<int> x;
x += 1,2,3,4,5;

vector<string> y(x.size());
transform(x.begin(), x.end(), y.begin(), lexical_cast<string>);

Error message:

error: no matching function for call to 'transform(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, <unresolved overloaded function type>)'

Which clearly indicates there is a problem with lexical_cast as the last argument to convert ... Is there a way to avoid creating a function object that wraps lexical_cast?

Thank!

+5
source share
1 answer

This is not verified, but you can try:

transform(x.begin(), x.end(), y.begin(), lexical_cast<string, int>);

lexical_castis a template with two template options. Usually the second output is inferred from type inference from the argument, but you do not provide an argument, so you need to explicitly specify it.

+7
source

All Articles