Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a function to copy an array in C/C++?

Tags:

c++

arrays

c

I am a Java programmer learning C/C++. So I know that Java has a function like System.arraycopy(); to copy an array. I was wondering if there is a function in C or C++ to copy an array. I was only able to find implementation to copy an array by using for loop, pointers,etc. Is there a function that I can use to copy an array?

like image 459
J L Avatar asked Apr 22 '13 01:04

J L


People also ask

Can you copy arrays in C?

1. C program to copy all elements of one array into another array. In this program, we need to copy all the elements of one array into another. This can be accomplished by looping through the first array and store the elements of the first array into the second array at the corresponding position.

Can an array be copied?

The array can be copied by iterating over an array, and one by one assigning elements. We can avoid iteration over elements using clone() or System. arraycopy() clone() creates a new array of the same size, but System.


1 Answers

Since you asked for a C++ solution...

#include <algorithm> #include <iterator>  const int arr_size = 10; some_type src[arr_size]; // ... some_type dest[arr_size]; std::copy(std::begin(src), std::end(src), std::begin(dest)); 
like image 171
Ed S. Avatar answered Sep 20 '22 15:09

Ed S.