开发者

Using boost::lexical_cast with std::transform

开发者 https://www.devze.com 2023-03-12 09:31 出处:网络
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>);

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&开发者_StackOverflow中文版lt;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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消