定义:
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++;
return result;
}
注意:
如果待拷贝的区间与原区间有重叠,则应该使用copy_backward。
返回:
返回指向目的区间的最后一个元素的迭代器
示例:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main () {
int myints[]={10,20,30,40,50,60,70};
vector<int> myvector;
vector<int>::iterator it;
myvector.resize(7);
copy ( myints, myints+7, myvector.begin() );
cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it;
cout << endl;
return 0;
}