merging two arrays together

What is the best way to merge two arrays into each other. They don't have to be sorted. just merge them together. I have the code below can you tell me if this the most efficient fastest way to do so ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  #include <iostream>

using namespace std;

int main()
{
   int a[] = {1,2,3,4,5};
   int b[] = {6,7,8,9,10};
   int c[10];
   
   for(int i=0; i<5; i++)
   {
      c[i] = a[i];   
   }
   
   for(int i=5; i<10; i++)
   {
      c[i] = b[i-5];   
   }
   
   
   for(int i=0; i<10; i++)
   {
      cout << c[i] << endl;  
   }
   
}
You've done it.

There are library functions you can use too. Or you could use iterators. Etc.

BTW, "merging" is a word that implies order. You've "appended" or "concatenated" the arrays.

Hope this helps.
What are some good library function to use for appending ?

Thank you !
http://www.cplusplus.com/reference/algorithm/copy/

1
2
3
  auto i =
  copy( a, a + 5, c );
  copy( b, b + 5, i );
1
2
3
4
5
  copy(
    b, b + 5, 
    copy(
      a, a + 5,
      c ) );

Etc.

Hope this helps.
Topic archived. No new replies allowed.