【问题标题】:Using boost::lexical_cast with std::transform将 boost::lexical_cast 与 std::transform 一起使用
【发布时间】:2011-09-14 05:08:42
【问题描述】:

g++ 不喜欢:

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: 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>)'

这清楚地表明 lexical_cast 作为 transform 的最后一个参数存在问题...有没有办法避免编写包装 lexical_cast 的函数对象?

谢谢!

【问题讨论】:

  • 在我的脑海中,您可能需要lexical_cast&lt;string, int&gt;,因为第二个模板参数没有参数推导。不过,lexical_cast 可能会过载,这将要求您使用强制转换来消除您想要的歧义。

标签: c++ boost transform lexical-cast


【解决方案1】:

这是未经测试的,但您可以尝试:

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

lexical_cast 是一个带有两个模板参数的模板。通常第二个是从参数的类型推导中推导出来的,但您没有提供参数,因此您需要显式指定它。

【讨论】:

  • 有道理。我应该看到的。谢谢!
猜你喜欢
  • 1970-01-01
  • 2020-08-03
  • 2016-04-22
  • 2016-09-04
  • 1970-01-01
  • 1970-01-01
  • 2020-04-04
  • 1970-01-01
  • 2021-06-06
相关资源
最近更新 更多