copy the middle of 2dim array into another array

0 down vote favorite


I want to copy the middle of 2dim array into another array. who knows how i can do it. for example I have:

int A[4][2] = {{1, 2} ,{5, 6} , {7, 8} , {3, 4} };

copy the second and third rows into another array to have the following array:

int B[4][2] = {{null, null} ,{5, 6} , {7, 8} , {null, null} };

What is the value of null in your case?
Either way, it's like copying any other C-style array, with std::copy or equivalent.
There are many ways to do the task. For example

1
2
3
4
5
6
7
8
9
10
11
int A[4][2] = {{1, 2} ,{5, 6} , {7, 8} , {3, 4} };
int B[4][2] = {};

std::accumulate( A + 1, A + 3, B + 1,
		 []( decltype( B + 1 ) b, decltype( *A ) &a)
                 {
			 return 
			 ( 
				std::copy( a, a + 2, *b ), ++b
			 );
                 } );

Last edited on
Topic archived. No new replies allowed.