How to Copy One Array to Another in C++? Last Updated : 03 Apr, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, arrays are a type of data structure that stores a fixed-size collection of elements of the same type in a contiguous memory location, and sometimes we need to copy the contents of one array to another. In this article, we will learn how to copy one array to another in C++. Example: Input: arr1 = {10, 20, 30, 40, 50, 60} Output: arr2: {10, 20, 30, 40, 50, 60}Copy All Elements of One Array to Another ArrayFor copying all the elements stored in one array to another in C++, we can use the std::copy() function provided in the STL <algorithm> library that copies a range of elements from one container to another. Syntax to Use std::copycopy(strt_iter1, end_iter1, strt_iter2)Here, strt_iter1 is the iterator to the beginning of the first array.end_iter1 is the iterator to the end of the first array.strt_iter2 is the iterator to the beginning of the second array where we have to copy the elements of the first array.C++ Program to Copy One Array to Another The below program demonstrates how we can copy one array to another using copy() function in C++. C++ // C++ program to illustrate how to copy one array to // another #include <algorithm> #include <iostream> using namespace std; // Driver code int main() { // First array int arr1[] = { 10, 20, 30, 40, 50, 60 }; int n = sizeof(arr1) / sizeof(arr1[0]); // Print the first array cout << "Array 1: "; for (int i = 0; i < n; i++) cout << arr1[i] << " "; cout << endl; // Second array to store the elements of the first array int arr2[n]; // Copy first array to second array copy(arr1, arr1 + n, arr2); // Print the second array cout << "Array 2: "; for (int i = 0; i < n; i++) cout << arr2[i] << " "; cout << endl; return 0; } OutputArray 1: 10 20 30 40 50 60 Array 2: 10 20 30 40 50 60 Time Complexity: O(n), where n is the number of elements being copied.Auxiliary Space: O(n) Comment More infoAdvertise with us Next Article How to Print an Array in C++? N nikitamehrotra99 Follow Improve Article Tags : C++ Programs C++ cpp-array Arrays CPP Examples +1 More Practice Tags : CPPArrays Similar Reads How to Copy a One Deque to Another in C++? In C++, STL provides a container called deque (short for double-ended queue) that allows fast insertion and deletion at both its beginning and its end. In some scenarios, we may need to copy the contents of one deque to another. In this article, we will learn how to copy one deque to another in C++. 2 min read How to Copy a Vector to an Array in C++? In C++, vectors are dynamic arrays that can grow and reduce in size as per requirements. Sometimes, we may need to copy the contents of a vector to the POD array. In this article, we will learn how to copy a vector to an array in C++. Example: Input: myVec = {10,20,30,40,50,60};Output: array: {10,20 2 min read How to Sort an Array in C++? Sorting an array involves rearranging its elements in a specific order such as from smallest to largest element or from largest to smallest element, etc. In this article, we will learn how to sort an array in C++.Example:Input: arr ={5,4,1,2,3}Output: 1 2 3 4 5Explanation: arr is sorted in increasin 4 min read How to Convert an Array to a Vector in C++? In this article, we will learn how to convert the given array into vector in C++.The simplest method to convert an array to vector is by using range constructor of vector. Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1, 3, 6, 2, 9}; 3 min read How to Print an Array in C++? In C++, an array is a fixed-size linear data structure that stores a collection of elements of the same type in contiguous memory locations. In this article, we will learn how to print an array in C++. For Example, Input: array = {10, 20, 30, 40, 50}Output: Array Elements: 10 20 30 40 50Printing Arr 2 min read How to Loop Over an Array in C++? In C++, an array is a data structure that stores elements of similar type in contiguous memory locations. We can access the elements of an array using array indexing. In this article, we will learn how to loop over an array in C++. Example: Input: int arr[] = [1, 2, 3, 4, 5] Output: Array Elements: 2 min read Like