### Assigning part of a 2d array to a new 2d array I am trying to do this, but it is giving me "initializer fails to determine size of ‘b’"

int main()
{

int a;
a = 1;
a = 1;

int b[] = a;

return 0;
} You cannot directly assign entire rows of arrays. You may use copy, see:

http://www.cplusplus.com/reference/algorithm/copy/?kw=copy

 ``12345678910111213`` ``````int main() { int a; a = 1; a = 1; int b; std::copy(a, a + 2, b); return 0; } `````` you can't do that in c++m you have to do it this way:
 ``123456`` ``````int b; for( int i = 0; i < 12; i++ ) { b[ i ][ 0 ] = a[ 0 ]; b[ i ][ 0 ] = a[ 1 ]; }``````
Last edited on @coder777

your code is wrong, here is the correct version:
 `` `` ``std::copy( a, a + 2, b );``

and you have to ` #include <algorithm> ` to use `std::copy`
Last edited on ``12345678910111213141516`` ``````#include #include using namespace std; int main() { vector< vector > a( 12, vector( 2 ) ); a = 1; a = 1; vector< vector > b( 12, vector( 2 ) ); b = a; for ( int i : a ) cout << i << ' '; for ( int i : b ) cout << i << ' '; }`````` Alternatively (if I understand what you are trying to do):

 ``123456789101112131415161718`` ``````#include #include struct Thing { int a, b; }; std::ostream& operator<<(std::ostream& os, const Thing& th) { return os << th.a << ", " << th.b; } int main() { std::vector stuff { {1, 1}, {1, 0}, {0, 1} }; Thing thing { stuff }; std::cout << thing << '\n'; thing = stuff; std::cout << thing << '\n'; }`````` fewdiefie wrote:
your code is wrong, here is the correct version:
So? And what is actually wrong with my code? It does what i intended: copy a row. Or in other words:

Assigning part of a 2d array to a new 2d array

While your code does not even compile. I suggest that you test the code before you judge it.

fewdiefie wrote:
and you have to #include <algorithm> to use std::copy
That's in the link. @fewdiefie
You forgot that `a` is a 2D array, so `a` is the address of a 1D row in that array.
Registered users can post here. Sign in or register to post.