C++

Is There a Function to Copy an Array in C++

When doing complex programming many times a person requires to copy an array or to create a new array with almost everything as used in the previous array, this can be done manually but it is a time taking process. So, if you are searching for a function that can do the copy job for you, follow this article’s guidelines.

Is There a Copy Function in C++ to Copy an Array

Yes, C++ does have a function to copy an array and it can be used in any C++ code. But remember that to use the copy function, a header file “<algorithm>” needs to be included in C++, as this header file supports the copy function in C++.

The syntax to use copy function is mentioned below:

Syntax

In the syntax, the arguments of the copy function are the arrays which need to be copied and the destination of the array:

std::copy (first, last, result)

 
The copy() function has three parameters:

    • first- array from where the elements will be copied
    • last- points the last index of the array to where we want to copy elements
    • result- Points the new array where we want to copy the elements

How to Use copy() Function in C++ Code

The complete process to copy an array using the copy() function is discussed in the below-mentioned steps:

Step 1: Firstly import the header files as you always do for any C++ code, remember that to use copy() function, the <algorithm> header file needs to be included with a <iostream> header file:

#include <iostream>
#include <algorithm>

 

Step 2: Then start the main function(), all of the main code will be inside this main function:

int main()
{  
 
 
    return 0;
}

 

Step 3: Now let’s create an array which we will copy using the copy command in the upcoming steps. Here, I have named my array as org[ ] to represent the original array, you can use any other name too. My array has 7 elements, so I have also declared a variable n=7 which is changeable depending upon the number of elements inside an array:

const int n = 7;
    int org[] = {2, 9, 8, 1, 3, 6, 3};

 

Step 4: Now I have defined a dest[ ] array, which is an array where the org[ ] array will be copied. After that finally use the copy function, the first argument is the original array, second is the array + n (which  is the size of array), lastly the dest which is the array where all the elements will be copied:

int dest[n];
    std::copy(org, org+n, dest);

 

Step 5: Now by running a for loop I will print arrays. The below for loop is to copy org array:

std::cout << "Original Array: ";
   
   for (int i=0; i<n; i++)
        std::cout << org[i] << ' ';
        std::cout << '\n';

 

Step 6: Similarly, to print the copied array I have used another for loop:

std::cout << "Copy of Array: ";
  for (int i=0; i<n; i++)
        std::cout << dest[i] << ' ';
        std::cout << '\n';

 

Note: I have only printed/displayed both the arrays just for understanding but it is not mandatory.

The complete code is given below:

Code

#include <iostream>
#include <algorithm>

 int main(){
    const int n = 7;
    int org[] = {2, 9, 8, 1, 3, 6, 3};
 
    int dest[n];
    std::copy(org, org+n, dest);
   
   std::cout << "Original Array: ";
   
   for (int i=0; i<n; i++)        
        std::cout << org[i] << ' ';
        std::cout << '\n';
       
  std::cout << "Copy of Array: ";
  for (int i=0; i<n; i++)
        std::cout << dest[i] << ' ';
        std::cout << '\n';
 
    return 0;
}

 

Now let’s run the code to display the output and to verify that array is copied successfully:

Output

Conclusion

Yes, there is a copy() function in C++ to copy arrays. To use copy() function just include <algorithm> header file at the top after which std::copy() can be used inside any C++ code. The syntax and a C++ code is also provided in the above-mentioned guidelines for easy learning.

About the author

Zahra Zamir

An Electronics graduate who loves to learn and share the knowledge, my passion for my field has helped me grasp complex electronics concepts and now I am here to share them with others.