转换stl中的数组像副本

fho*_*fho 2 c++ stl copy vector

现在是时候了另一个'我怎么用c ++做这件事而不放弃我的抓地力'问题!

这次:

考虑以下来自cplusplus.com的代码:

template<class InputIterator, class OutputIterator>
  OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result )
{
  while (first!=last) *result++ = *first++;
  return result;
}
Run Code Online (Sandbox Code Playgroud)

有没有办法转换*first为类型*result

换句话说:有没有办法确定(在编译时)结果的类型?

Arm*_*yan 6

是的,是的类型*result('因为类型resultOutputIterator)

typename std::iterator_traits<OutputIterator>::value_type
Run Code Online (Sandbox Code Playgroud)

当然,如果OutputIterator是指针或正确编写的STL兼容迭代器.否则,不,我认为没有办法.在即将推出的C++ 0x中,它会更容易,也就是说,

decltype(*result)
Run Code Online (Sandbox Code Playgroud)

HTH,

阿尔钦