. Advertisement .
..3..
. Advertisement .
..4..
Arrays are assigned to store various objects of similar data types together at adjacent memory zones. We use arrays specifically in the programs and the operations become useful during doing some particular tasks. In this post, we will refer to you how to copy an array in C++ and even provide different programs using various solutions to make it.
To copy an array in C++
In reality, there are four various solutions called shallow copy, deep copy, copy() function, and copy_backward() function.
1. Shallow copy
When it comes to the shallow copy, we copy the destination array’s pointer to the source array pointer. It turns the pointers point at a similar memory zone. Here, the real elements are available in the memory zone in a single area. However, we get double array pointers that point at a similar zone.
As for this scenario, if we transform the elements in any of the arrays, those alterations are shown in another array.
The code for making the shallow copy of the array in C++ is provided below
Code:
#define SIZE 10
int main() {
int arr1[SIZE];
for(int i=0;i<SIZE;i++)
arr1[i]=i;
cout<<"Array 1 elements: ";
for(int i=0; i<SIZE;i++)
cout<<arr1[i]<<" ";
cout<<endl;
int *arr2;
arr2 = arr1;
cout<<"Array 2 elements: ";
for(int i=0; i<SIZE;i++)
cout<<arr2[i]<<" ";
cout<<endl;
cout<< "Changing element at index 3 in the second array to 100.";
cout<<endl;
arr2[3]=100;
cout<<"Printing both the arrays after change";
cout<<endl;
cout<<"Array 1 elements: ";
for(int i=0; i<SIZE;i++)
cout<<arr1[i]<<" ";
cout<<endl;
cout<<"Array 2 elements: ";
for(int i=0; i<SIZE;i++)
cout<<arr2[i]<<" ";
cout<<endl;
return 0;
}
Output:
Array 1 elements: 0 1 2 3 4 5 6 7 8 9
Array 2 elements: 0 1 2 3 4 5 6 7 8 9
Changing element at index 3 in the second array to 100.
Printing both the arrays after change
Array 1 elements: 0 1 2 100 4 5 6 7 8 9
Array 2 elements: 0 1 2 100 4 5 6 7 8 9
2. Deep copy
Deep copy is particularly distinctive in the path which makes the real copy of array elements at various zone. Every copy of the array is not dependent on the other, and the transformations in a single array are not displayed in other arrays.
Next, the deep copy can copy array elements to various zone in the memory, thus, staying away from the pointers’ conflict. However, a deep copy accommodates a lot of space since elements are copied to various zone each time we make a deep copy of the array.
The code for making the deep copy of the array in C++ is provided below
Code:
#define SIZE 10
int main() {
int arr1[SIZE];
for(int i=0;i<SIZE;i++)
arr1[i]=i;
cout<<"Array 1 elements: ";
for(int i=0; i<SIZE;i++)
cout<<arr1[i]<<" ";
cout<<endl;
int arr2[SIZE];
for(int i=0;i<SIZE;i++)
arr2[i]=arr1[i];
cout<<"Array 2 elements: ";
for(int i=0; i<SIZE;i++)
cout<<arr2[i]<<" ";
cout<<endl;
cout<< "Changing element at index 3 in second array to 100.";
cout<<endl;
arr2[3]=100;
cout<<"Printing both the arrays after change";
cout<<endl;
cout<<"Array 1 elements: ";
for(int i=0; i<SIZE;i++)
cout<<arr1[i]<<" ";
cout<<endl;
cout<<"Array 2 elements: ";
for(int i=0; i<SIZE;i++)
cout<<arr2[i]<<" ";
cout<<endl;
return 0;
}
Output:
Array 1 elements: 0 1 2 3 4 5 6 7 8 9
Array 2 elements: 0 1 2 3 4 5 6 7 8 9
Changing element at index 3 in second array to 100.
Printing both the arrays after change
Array 1 elements: 0 1 2 3 4 5 6 7 8 9
Array 2 elements: 0 1 2 100 4 5 6 7 8 9
3. Use the copy() function
The copy() function is the suggested method of copying range-based structures with a single function call.copy() gets the first and final elements of range and the start of the destination array. Be careful that you can get undefined behavior if the third parameter is in the source range.
Code:
int main() {
vector<string> str_vec = { "Warty", "Hoary",
"Breezy", "Dapper",
"Edgy", "Feisty" };
vector<string> new_vec(str_vec.size());
copy(str_vec.begin(), str_vec.end(), new_vec.begin());
cout << "new_vec - | ";
copy(new_vec.begin(), new_vec.end(),
std::ostream_iterator<string>(cout," | "));
cout << endl;
return EXIT_SUCCESS;
}
Output:
new_vec - | Warty | Hoary | Breezy | Dapper | Edgy | Feisty |
4. Use the copy_backward() function
The copy_backward() solution might copy the array in reverse order with the original element, but the order can be preserved. When copying the overlapping ranges, here is a thumb’s rule you can remember during taking the solutions std::copy, and std::copy_backward: copy is suitable when copying to the left. On the other hand, copy_backward is suitable when copying to the right.
Code:
#define SIZE 10
int main() {
int arr1[SIZE];
for(int i=0;i<SIZE;i++)
arr1[i]=i;
cout<<"Array 1 elements: ";
for(int i=0; i<SIZE;i++)
cout<<arr1[i]<<" ";
cout<<endl;
int arr2[SIZE];
copy_backward(arr1, arr1+SIZE, arr2+SIZE);
cout<<"Array 2 elements: ";
for(int i=0; i<SIZE;i++)
cout<<arr2[i]<<" ";
return 0;
}
Output:
Array 1 elements: 0 1 2 3 4 5 6 7 8 9
Array 2 elements: 0 1 2 3 4 5 6 7 8 9
Wrapping It Up
There is a wide range of scenarios in which we can copy the data from one to another array. After browsing around this post, you can accumulate more knowledge related to various solutions to copy an array in C++.
Leave a comment