C++ concatenate two int arrays into one larger array
Use std::copy
defined in the header <algorithm>
. The args are a pointer to the first element of the input, a pointer to one past the last element of the input, and a pointer to the first element of the output.
( https://en.cppreference.com/w/cpp/algorithm/copy )
int * result = new int[size1 + size2];
std::copy(arr1, arr1 + size1, result);
std::copy(arr2, arr2 + size2, result + size1);
Just suggestion, vector
will do better as a dynamic array rather than pointer
Fastest way to append two arrays ( concatenate two arrays) C++
First of all, I think your loops are fine. If you want to, you can try if merging them into one loop gives you any speed-up because you only need half the iterations:
for (int i = 0; i < 5; ++i) {
c[i] = a[i];
c[i+5] = b[i];
}
But measure this, it is by no means guaranteed to be faster. If the arrays always are that small, any difference in performance is highly unlikely.
If you want to use the <algorithm>
library (which I like to do for readability, but in your case the loops are short enough), you can do it like this:
std::copy(a, a+5, c);
std::copy(b, b+5, c+5);
It most likely is not faster than the loops, but looks cleaner IMO. When the amount of data gets bigger, it might actually offer a speedup by being implemented in an optimized way, but as always, measure it.
I would stay away from memcpy
though. It does not work on all types and offers no (or should not offer any) advantage over std::copy
.
As a last remark, consider replacing the raw C-arrays with the modern std::array
. One advantage here is that you can just copy it with =
, plus some neat member functions like fill
and size
.
How do I concatenate two arrays in C#?
var z = new int[x.Length + y.Length];
x.CopyTo(z, 0);
y.CopyTo(z, x.Length);
Combining two int's arrays into one array by order not working
All we need is for
loop:
static void Merge(int[] left, int[] right)
for (int indexLeft = 0, indexRight = 0;
indexLeft < left.Length || indexRight < right.Length;)
if (indexRight >= right.Length ||
(indexLeft < left.Length && left[indexLeft] <= right[indexRight]))
Console.WriteLine(left[indexLeft++]);
else
Console.WriteLine(right[indexRight++]);
}
we move by both left
and right
arrays (indexLeft
, indexRight
) and we print item from left
array if and only if
- We exhausted
right
array:indexRight >= right.Length
. - If we have items in both
left
andright
arrays andleft[indexLeft] <= right[indexRight]
otherwise we print right
array item.
Edit: If you want to return merged array (result
):
static int[] Merge(int[] left, int[] right) {
int[] result = new int[left.Length + right.Length];
int index = 0;
for (int indexLeft = 0, indexRight = 0;
indexLeft < left.Length || indexRight < right.Length;)
if (indexRight >= right.Length ||
(indexLeft < left.Length && left[indexLeft] <= right[indexRight]))
result[index++] = left[indexLeft++];
else
result[index++] = right[indexRight++];
return result;
}
How can I concat two dynamic int-arrays in C?
realloc() returns a new pointer (usually) unless the new size is only slightly larger and can fit in an over-allocated array. You are not passing the new pointer 'numbers1' back to the calling function.
How to append two arrays in C language?
Your existing code is allocating the wrong amount of memory because it doesn't take sizeof(float)
into account at all.
Other than that, you can append one array to the other with memcpy
:
float x[4] = { 1, 1, 1, 1 };
float y[4] = { 2, 2, 2, 2 };
float* total = malloc(8 * sizeof(float)); // array to hold the result
memcpy(total, x, 4 * sizeof(float)); // copy 4 floats from x to total[0]...total[3]
memcpy(total + 4, y, 4 * sizeof(float)); // copy 4 floats from y to total[4]...total[7]
Related Topics
Static and Dynamic/Shared Linking with Mingw
How Are User-Level Threads Scheduled/Created, and How Are Kernel Level Threads Created
<Random> Generates Same Number in Linux, But Not in Windows
Cmath VS Math.H (And Similar C-Prefixed VS .H Extension Headers)
Why Was Std::Pow(Double, Int) Removed from C++11
Is Returning References of Member Variables Bad Practice
What Is the Comdat Section Used For
Printing Double Without Losing Precision
C++11 Anonymous Union with Non-Trivial Members
Efficiently Reading a Very Large Text File in C++
Std::Lexical_Cast - Is There Such a Thing
How to Pass Derived Classes by Reference to a Function Taking Base Class as a Parameter
Pros & Cons of Putting All Code in Header Files in C++
Why Does This Simple Std::Thread Example Not Work
Reading an Application's Manifest File