Copy STL Algorithm
The copy()algorithm copies a sequence beginning at start and ending with end, putting the result into the sequence pointed to by result. It returns a pointer to the end of the resulting sequence. The range to be copied must not overlap with result.
Syntax:
template <class InputIterator, class OutputIterator> OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result ); Copy range of elements Copies the elements in the range [first,last) into a range beginning at result. Returns an iterator to the last element in the destination range.
Implementation of copy algorithm
#include <iostream> #include <algorithm> #include <vector> using namespace std; typedef int type; int main () { int list[]={1,2,4,7,8,9,10}; vector<type> vec0(7); vector<type>::iterator it; cout << "contains befor the copy:"; for (it=vec0.begin(); it!=vec0.end(); ++it) cout << " " << *it; cout << endl; copy ( list, list+7, vec0.begin() ); cout << "contains after the copy:"; for (it=vec0.begin(); it!=vec0.end(); ++it) cout << " " << *it; cout << endl; return 0; }//end of main
Program Output:
contains befor the copy: 0 0 0 0 0 0 0 contains after the copy: 1 2 4 7 8 9 10