函数原型:
template <class InputIteraton, class OutputIterator>OutputIterator copy( InputIterator first, InputIterator last, OutputIterator result);
解释:
copy区间[first,last)到result后。返回目标区间的最后一个元素的迭代器。
代码可能类似于:
template<class InputIterator, class OutputIterator>OutputIterator copy( InputIterator first, InputIterator last, OutputIterator result){while (first!=last) *result++ = *first++;
return result;
}
程序示例:
//copy algorithm example
#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); //allocate space for 7 elements
copy(myints,myints+7,myvector.begin());cout << "myvector contains:";
for ( it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it;
cout << endl;return 0;
}
Output:
myvector contains: 10 20 30 40 50 60 70 |