C++ reference: copy

参考自:http://www.cplusplus.com/reference/algorithm/copy/

copy


#include<algorithm>
template <class InputIterator, class OutputIterator>
  OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result);

将范围[first,last)中的元素复制到从result开始的位置。返回一个迭代器指向目标范围的末尾(即复制的最后一个元素的后面)。

  •  实现
template<class InputIterator, class OutputIterator>
  OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result)
{
  while (first!=last) {
    *result = *first;
    ++result; ++first;
  }
  return result;
}
  •  示例
#include <iostream>     // std::cout
#include <algorithm>    // std::copy
#include <vector>       // std::vector

int main () {
  int myints[]={10,20,30,40,50,60,70};
  std::vector<int> myvector (7);

  std::copy ( myints, myints+7, myvector.begin() );

  std::cout << "myvector contains:";
  for (std::vector<int>::iterator it = myvector.begin(); it!=myvector.end(); ++it)
    std::cout << ' ' << *it;

  std::cout << '\n';

  return 0;
}/*Output
myvector contains: 10 20 30 40 50 60 70
*/

猜你喜欢

转载自blog.csdn.net/wingrez/article/details/82720708
今日推荐