make a copy of an array c++ code example

Example: array copx c++

#include <algorithm> //Only needed for Option 1
#include <iostream>

using namespace std;

int main() {
  	//Option 1
    const int len{3};
    int arr1[len] = {1,2,3};
    int arr2[len]; //Will be the copy of arr1
    copy(begin(arr1), end(arr1), begin(arr2));
  
  	//Use the following, if you are not using namespace std;
    //std::copy(std::begin(arr), std::end(arr), std::begin(copy));
  
    //Option 2
    int arr3[len]; //Will be the copy of arr1
    for(int i = 0; i<len; ++i) {
      arr3[i] = arr1[3];
    }
    
    return 0; //exitcode
};

Tags:

Cpp Example