Matrices and Vectors

Hey guys, I'm new to vectors and I'm trying to do something but I'm completely stuck.

I have 2 multidimensional arrays (matrices) with 3 rows and 5 cols so it kind of looks like
1
2
-----
-----

1
2
--0--
-----


what I want to do is push them to a vector and use the vector to print them out.

Here's what I have so far:
1
2
3
4
5
6
7
8
9
10
11
12
int main ()
{
  char m_board[3][5] = {{'-','-','-','-','-'},{'-','-','-','-','-'},{'-','-','-','-','-'}};
  char m_board2[3][5] = {{'-','-','-','-','-'},{'-','0','-','-','-'},{'-','-','-','-','-'}};
  std::vector<char> char_vect (m_board, m_board + 5 );

  for (std::vector<char>::iterator it = char_vect.begin(); it != char_vect.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
 }


I know this won't work because this code reads in one matrix and creates a matrix of vectors, I need a vector of matrices. So I need each matrix to be pushed as one item in a vector, then the other one, then the vector prints one at a time.

Does that make any sense or am I just going completely insane? Can someone help, I'm totally stumped?
You need a vector of vectors.

If you are using c++11, the code is pretty much the same:
 
std::vector< std::vector< char >> m_board = {{'-','-','-','-','-'},{'-','-','-','-','-'},{'-','-','-','-','-'}};


Otherwise:
1
2
3
4
5
char arr[2][3] = { {'1', '2', '3' }, { '4', '5', '6' } };
std::vector< std::vector< char > > char_vect;

for ( int i = 0; i < 2; ++i )
  char_vect.push_back( std::vector< char >( arr[i], arr[i] + 3 ) );


Last edited on
I think this is exactly what I was looking for. Thank you!
Topic archived. No new replies allowed.