Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using boost::lexical_cast with std::transform

g++ doesn't 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>);

The error message is:

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 an issue with lexical_cast as the last argument to transform... Is there a way to avoid writing a function object that wraps lexical_cast?

Thanks!

like image 391
Frank Avatar asked Jun 13 '11 20:06

Frank


1 Answers

This is untested, but you could try:

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

lexical_cast is a template with two template parameters. Normally the second one is deduced from type deduction from the argument, but you aren't providing an argument, so you need to explicitly specify it.

like image 101
Peter Alexander Avatar answered Sep 29 '22 03:09

Peter Alexander