copy

C++ Reference

copy
Syntax:
  #include <algorithm>
  iterator copy( iterator start, iterator end, iterator dest );

The copy() function copies the elements between start and end to dest. In other words, after copy() has run,

 *dest == *start
 *(dest+1) == *(start+1)
 *(dest+2) == *(start+2)
 ...
 *(dest+N) == *(start+N)                

The return value is an iterator to the last element copied. copy() runs in linear time.

For example, the following code uses copy() to copy the contents of one vector to another:

 vector<int> from_vector;
 for( int i = 0; i < 10; i++ ) {
   from_vector.push_back( i );
 }              

 vector<int> to_vector(10);               

 copy( from_vector.begin(), from_vector.end(), to_vector.begin() );             

 cout << "to_vector contains: ";
 for( unsigned int i = 0; i < to_vector.size(); i++ ) {
   cout << to_vector[i] << " ";
 }
 cout << endl;            
Related topics: