Different methods to copy in C++ STL | std::copy(), copy_n(), copy_if(), copy_backwards()

Various varieties of copy() exist in C++ STL that allows to perform the copy operations in different manners, all of them having their own use. These all are defined in header <algorithm>. This articles introduces everyone to these functions for usage in day-to-day programming.

1. copy(strt_iter1, end_iter1, strt_iter2) : The generic copy function used to copy range of elements from one container to other. It takes 3 arguments:

  • strt_iter1 : The pointer to the beginning of source container, from where elements have to be started copying.
  • end_iter1 : The pointer to the end of source container, till where elements have to be copied.
  • strt_iter2 : The pointer to the beginning of destination container, to where elements have to be started copying.

2. copy_n(strt_iter1, num, strt_iter2) : This version of copy gives the freedom to choose how many elements have to be copied in destination container. IT also takes 3 arguments:

  • strt_iter1 : The pointer to the beginning of source container, from where elements have to be started copying.
  • num : Integer specifying how many numbers would be copied to destination container starting from strt_iter1. If a negative number is entered, no operation is performed.
  • strt_iter2 : The pointer to the beginning of destination container, to where elements have to be started copying.
filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code to demonstrate the working of copy()
// and copy_n()
  
#include<iostream>
#include<algorithm> // for copy() and copy_n()
#include<vector>
using namespace std;
  
int main()
{
      
   // intializing source vector 
   vector<int> v1 = { 1, 5, 7, 3, 8, 3 };
     
   // declaring destination vectors
   vector<int> v2(6);
   vector<int> v3(6);
     
   // using copy() to copy 1st 3 elements
   copy(v1.begin(), v1.begin()+3, v2.begin());
     
   // printing new vector
   cout << "The new vector elements entered using copy() : ";
   for(int i=0; i<v2.size(); i++)
   cout << v2[i] << " ";
     
   cout << endl;
     
   // using copy_n() to copy 1st 4 elements
   copy_n(v1.begin(), 4, v3.begin());
     
   // printing new vector
   cout << "The new vector elements entered using copy_n() : ";
   for(int i=0; i<v3.size(); i++)
   cout << v3[i] << " ";
    
}

chevron_right


Output:

The new vector elements entered using copy() : 1 5 7 0 0 0 
The new vector elements entered using copy_n() : 1 5 7 3 0 0 

3. copy_if() : As the name suggests, this function copies according to result of a “condition“.This is provided with the help of a 4th argument, a function returning a boolean value.
This function takes 4 arguments, 3 of them similar to copy() and additional function, which when returns true, a number is copied, else number is not copied.

4. copy_backwards() : This function starts copying elements into the destination container from backwards and keeps on copying till all numbers are not copied. The copying starts from the “strt_iter2” but in backward direction. It also takes similar arguments as copy().

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code to demonstrate the working of copy_if()
// and copy_backward()
  
#include<iostream>
#include<algorithm> // for copy_if() and copy_backward()
#include<vector>
using namespace std;a
  
int main()
{
      
    // intializing source vector 
    vector<int> v1 = { 1, 5, 6, 3, 8, 3 };
          
    // declaring destination vectors
    vector<int> v2(6);
    vector<int> v3(6);
          
    // using copy_if() to copy odd elements
    copy_if(v1.begin(), v1.end(), v2.begin(), [](int i){return i%2!=0;});
          
    // printing new vector
    cout << "The new vector elements entered using copy_if() : ";
    for(int i=0; i<v2.size(); i++)
    cout << v2[i] << " ";
          
    cout << endl;
          
    // using copy_backward() to copy 1st 4 elements
    // ending at second last position
    copy_backward(v1.begin(), v1.begin() + 4, v3.begin()+ 5);
          
    // printing new vector
    cout << "The new vector elements entered using copy_backward() : ";
    for(int i=0; i<v3.size(); i++)
    cout << v3[i] << " ";
  
}

chevron_right


Output:

The new vector elements entered using copy_if() : 1 5 3 3 0 0 
The new vector elements entered using copy_backward() : 0 1 5 6 3 0 

This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.