1
int * result = new int[size1 + size2];
copy(arr1, arr1 + size1, result);
copy(arr2, arr2 + size2, result + size1);

Taken from: https://stackoverflow.com/a/12791344/6268615

Can someone explain more detail on the parameter? I've gone through the documentation of c++ but still don't understand it.

This code is merging two arrays into one dynamic arrays by using copy function.

Anonymous
  • 71
  • 7
  • “This code is merging two arrays into one dynamic arrays by using copy function.” Sounds like your understood it already. What is your question? – Henri Menke Sep 03 '18 at 06:42
  • Hi thanks for responding, I don't understand about the parameter that taken into the functions. – Anonymous Sep 03 '18 at 06:43
  • I recommend you draw out the arrays involved on paper, especially `result`. Then use arrows for the pointers you have, especially `result + size1`. – Some programmer dude Sep 03 '18 at 06:44
  • You can also do the same thing with `copy(arr2, arr2 + size2, copy(arr1, arr1 + size1, result));` because `copy` returns the position after the just copied elements. Does that help? Probably not. – john Sep 03 '18 at 06:51
  • you should read about dynamic allocation of memory ,here with the help of new operator ,memory is allocated equal to the sizeof size1 + size2 and the copy function takes 3 parameters first base address ,second address of location in array till where we have to copy it,and third argument is where should it be copied means address of location in resultant array. – Shivanshu Sep 03 '18 at 06:58

2 Answers2

2

Basically this is what the parameter of copy mean:

copy( StartPositionSource, EndPositionSource, StartPositionTarget )

You copy from Source to Target.

Given your Example:

copy(arr1, arr1 + size1, result);
copy(arr2, arr2 + size2, result + size1);

and assuming arr1 has 2 elements and arr2 has 3 elements you get:

result = [arr1[0], arr1[1], arr2[0], arr2[1], arr2[2]]
Mailerdaimon
  • 6,003
  • 3
  • 35
  • 46
0

The dynamic array pointed to by result is large enough to contain both arr1 and arr2 one after the other. The first copy:

copy(arr1, arr1 + size1, result);

copies each element in the range [arr1, arr1 + size1) starting from the position in the dynamic array pointed to by result, i.e., the first element of the dynamic array.

Once arr1 has been copied into [result, result + size1), you want to copy arr2 starting from result + size1. This is achieved by means of:

copy(arr2, arr2 + size2, result + size1);
Ilio Catallo
  • 3,152
  • 2
  • 22
  • 40
  • `result` is not an array. – Swordfish Sep 03 '18 at 07:31
  • Ops, you are right, the array has been dynamically allocated. Corrected :) thank you! – Ilio Catallo Sep 03 '18 at 07:34
  • "the array has been dynamically allocated." there is no such thing like a "dynamically allocated array". – Swordfish Sep 03 '18 at 07:36
  • How would you define a heap-allocated, anonymous array (as in `new int[size1 + size2]`)? – Ilio Catallo Sep 03 '18 at 07:37
  • allocated memory – Swordfish Sep 03 '18 at 07:38
  • @Swordfish: Yes and no. The memory is allocated to hold a collection of elements identified by an index, which is exactly the definition of "array". – Mailerdaimon Sep 03 '18 at 07:54
  • Or look at https://en.cppreference.com/w/cpp/container/vector : 1) std::vector is a sequence container that encapsulates dynamic size arrays. – Mailerdaimon Sep 03 '18 at 07:55
  • Skimming through the C++14 draft, 3.7.4 reads _"Objects can be created dynamically during program execution (1.9), using new-expression (5.3.4)"_. Section 11.2.2 in "The C++ Programming Language" reads: _"Arrays of objects can also be created using new."_. The [StackOverflow own FAQ on arrays](https://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c) adopts the term _dynamic arrays_ when referring to expressions such as `new int[N]`. – Ilio Catallo Sep 03 '18 at 08:15
  • I still don't feel comfortable calling a pinter an array. – Swordfish Sep 03 '18 at 08:18
  • Why would you call a pointer an array? A pointer is a way to access an array. That is totally different. – Mailerdaimon Sep 03 '18 at 08:19
  • 1
    I am not saying that `result` is an array. Your first comment still applies, `result` is a pointer, which retains the memory address of the first element of a heap-allocated, anonymous array of size `size1 + size2`. – Ilio Catallo Sep 03 '18 at 08:20