Development

How to Copy an Array in C++

Captain Salem 3 min read

How to Copy an Array in C++

Arrays are some of the most basic data structures in C++ and any other programming language. They provide a diverse and efficient method of presenting related data without compromising performance.

However, we may encounter scenarios where we need to copy the contents of one array into another array. Luckily, C++ provides us with various methods and techniques to achieve this.

In this tutorial, we will explore all the methods and techniques we can use to copy one array into another and discuss the pros and cons of each method.

Method 1 - Manual Iteration

The most basic method is loop iteration. We can use a for loop to iterate over each element of the source array and append it to the destination array.

An example is as shown:

//
//  main.cpp
//  cpp
//
//  Created by captain salem on 2023-08-10.
//

#include <iostream>
using namespace std;

int main() {
    int src[] = {1, 2, 3, 4, 5};
    int dest[5];

    for (int i = 0; i < 5; i++) {
        dest[i] = src[i];
    }

    for (int i = 0; i < 5; i++) {
        cout << dest[i] << "\n";
    }

    return 0;
}

As you can see in the example, we use a classic for loop to iterate over the elements of the src array and append them to the dest array using indexing.

The resulting output is as shown:

1
2
3
4
5

Advantages

Using the above methods provides the following advantages.

  • Very classic and easy to implement
  • This method does not require any external libraries.

Drawbacks

It does, however, come with its disadvantages. These include:

  • It is not very efficient when working with large arrays.
  • This technique is not very well suited to handle dynamic arrays.

Method 2 - Using std::copy

The next method we can use is the std::copy function from the <algorithm> header file in the C++ standard library.

An example usage is as shown below:

//
//  main.cpp
//  cpp
//
//  Created by captain salem on 2023-08-10.
//
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int src[] = {1, 2, 3, 4, 5};
    int dest[5];

    copy(begin(src), end(src), dest);

    for (int i : dest) {
        cout << i << "\n";
    }
    return 0;
}

This method allows us to use the copy method to copy the elements of the array from the specified begin and end parameters.

Advantages

This method provides the following advantages:

  • Efficient for larger arrays.
  • Works with other containers like std::vector.

Drawbacks

It also comes with a disadvantage.

  • Requires the inclusion of the <algorithm> header.

Method 3 - Using std::memcpy

We can also use the std::memcpy function from the cstring header. The function copies memory byte-by-byte.

An example demonstration is as shown:

//
//  main.cpp
//  cpp
//
//  Created by captain salem on 2023-08-10.
//
#include <iostream>
#include <cstring>
using namespace std;

int main() {
    int src[] = {1, 2, 3, 4, 5};
    int dest[5];

    memcpy(dest, src, sizeof(src));

    for (int i : dest) {
        cout << i << "\n";
    }

    return 0;
}

The advantages and disadvantages of this method are outlined as shown:

Advantages

  • Very efficient for large arrays.
  • Direct memory copying.

Drawbacks

  • It can be unsafe if not used properly.
  • Requires the inclusion of the <cstring> header.

Method 4 - Using Array Initialization

If you know the elements of the source array at compile time, you can use the Array initialization technique to copy the contents of the array as demonstrated in the example below:

//
//  main.cpp
//  cpp
//
//  Created by captain salem on 2023-08-10.
//
#include <iostream>
using namespace std;

int main() {
    int src[] = {1, 2, 3, 4, 5};
    int dest[] = {src[0], src[1], src[2], src[3], src[4]};

    for (int i : dest) {
        cout << i << "\n";
    }

    return 0;
}

As you can see, we can walk over each index and copy it to the destination array.

Advantage

  • Simple syntax for known arrays.

Drawback

  • Not dynamic; it only works for known source arrays at compile time.

Method 5 - Using C++ Containers

A more flexible and built-in method of copying an array is using C++ container like std::vector. This method can be very efficient if you’re not strictly bound to using raw arrays.

An example code is as shown:

//
//  main.cpp
//  cpp
//
//  Created by captain salem on 2023-08-10.
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> src = {1, 2, 3, 4, 5};
    vector<int> dest = src;

    for (int i : dest) {
        cout << i << "\n";
    }

    return 0;
}

Advantages

  • Dynamic sizing.
  • Automatic memory management.
  • Built-in copy mechanism.

Drawbacks

  • Slightly more overhead than raw arrays.

Conclusion

In this tutorial, we explored the various methods and techniques to copy a source array to a destination array in C++. We also explored the pros and cons of each method to help you determine which method is most efficient for your use case.

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.