24

This is the same question asked in C# but i need for C++

How can I copy a part of an array to another array?

Consider I'm having

    int[] a = {1,2,3,4,5};

Now if I give the start index and end index of the array a it should get copied to another array.

Like if I give start index as 1 and end index as 3, the elements 2, 3, 4 should get copied in the new array.

In C# it is done as following

     int[] b = new int[3];
    Array.Copy(a, 1, b, 0, 3);

Is there any simple way like this to do the same task in C++?

1
  • 3
    Even better, use std::vector instead of the arrays in the first place. It has a constructor that does what you're doing, among many other great features. Commented Jun 19, 2012 at 13:37

6 Answers 6

25

Yes, use std::copy:

std::copy(a + src_begin_index,
          a + src_begin_index + elements_to_copy,
          b + dest_begin_index);

The equivalent of your C# example would be:

std::copy(a + 1, a + 4, b);
Sign up to request clarification or add additional context in comments.

3 Comments

Just make sure you don't overflow the bounds of your destination!
I'd go for copy_n... std::copy_n( a+src_begin_index, elements_to_copy, b+dst_begin_index )
A great answer indeed. To suplement this, memcopy isn't even faster than copy: stackoverflow.com/questions/4707012/…
6

Assuming you want a dynamically-allocated array as in the C# example, the simplest way is:

std::vector<int> b(a.begin() + 1, a.begin() + 4);

This also has the advantage that it will automatically release the allocated memory when it's destroyed; if you use new yourself, then you'll also need to use delete to avoid memory leaks.

Comments

2

For simple C-style arrays you can use memcpy:

memcpy(b, &a[1], sizeof(int) * 3);

This line copies sizeof(int) * 3 bytes (i.e. 12 bytes) from index 1 of a, with b as a destination.

Comments

1

There is the C memcpy command, which you can use like this:

memcpy(destinationArray, sourceArray, sizeof(*destinationArray) * elementsToCopy);

There is also std::copy, which is a more C++ way of doing it:

std::copy(source, source + elementsInSource, destination);

Note that neither of these functions check to make sure that enough memory has been allocated, so use at your own risk!

2 Comments

The use of std:copy is incorrect, as the the second argument should be one-past-the-end (source+elementsInSource), without the -1
@DaveS ok, thanks. I haven't used C++ in a while, so thanks for pointing that out.
1

Yes, using st standard library algorithm copy:

#include <algorithm>

int main()
{
  int array[5] = { 1,2,3,4,5 }; // note: no int[] array
  int *b = new int[3];
  std::copy(array+1, array+4, b); 
  // copies elements 1 (inclusive) to 4 (exclusive), ie. values 2,3,4
}

Comments

0

If you are using vector in CPP, you can do it this way -

Suppose you want to copy elements from arr from index A to index B, you can do it this way -

vector<int>nums(arr.begin()+A, arr.begin()+B+1);

Note - nums will be created as A[start, end) so we need to add extra 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.