printing matrix in a square form

Ok guys..nice weekend to all you of this forum.
let's say i have a 2d matrix or a 2d vector... how do i make it print in a rectangular form?...so i have a matrix...array or 2d vector be it...
mat = {{1,2,3,4,4,},
{7,4,5,7,6,},
{4,0,7,9,0,},};
when i call the usual for loop for print out..or when i write to file ...it prints a line...1,2,3,4,4,7,4,5,7, ecc ecc...
but i will like to print it on the screen or write it to file like this

1,2,3,4,4,
7,4,5,7,6,
4,0,7,9,0,

with commas or not...
I reckon the method may be different for a matrix and a 2d vector...lessons for all two methods are very welcomed..with examples and comments..thank you very much in advance...

Last edited on
I do not understand the problem. Is it so difficult to print out std::endl or '\n' after printing each row?!
just do an example @vlad..for a vector i see it..for an array matrix..using a for loop is not that clear..pls just do an example...
Here you are

std::cout << std::endl;
Last edited on
great example vlad..very great
1
2
3
4
5
6
7
8
9
10
11
12
std::vector<std::vector<int>> v = 
{ 
   { 1, 2, 3, 4, 4, },
   { 7, 4, 5, 7, 6, },
   { 4, 0, 7, 9, 0, },
};

for ( const std::vector<int> &v1 : v )
{
   for ( int x : v1 ) std::cout << x << ' ';
   std::cout << std::endl;
}

Last edited on
Topic archived. No new replies allowed.