How to Print std::array

Hi,

the following code snippet is trying to print a std::array container generically. It is printing every element consecutively like the following

1 2 3 4 5 6 7 8 9 10 11 12

I couldn't find a way on how to format. I'll appreciate if someone could shed a light on how to print every row in a line.
1 2 3
4 5 6
7 9 9
10 11 12

Thanks
Sabetay
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
28
#include <iostream>
using namespace std; 
#include <type_traits>
#include <array> 

using TCount0 = array<int, 3>; 
using TCount1 = std::array<TCount0, 2>;
using TCount2 = std::array<TCount1, 2>;

template<class T, std::size_t N>
struct is_array<std::array<T, N>> : std::true_type {};

template <typename T>
void PrintArray(T& A) {
    if (is_array<T>::value) 
        for(auto& e : A) 
            PrintArray(e);
}
template<>
void PrintArray(int& e)
{
    cout << e << " ";
}
int main() 
{
    TCount2 Cnt = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; 
    PrintArray(Cnt);
}

Last edited on
I believe you're looking for something like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <iomanip>
#include <array> 

template<typename T>
void PrintArray(const T& e) {
    std::cout << std::setw(3) << e << ' ';
}

template<typename T, std::size_t N>
void PrintArray(const std::array<T,N>& A) {
    for (const auto& e : A) 
        PrintArray(e);
    std::cout << '\n';
}

int main()  {
    std::array<std::array<std::array<int,3>,2>,2>
        Cnt = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
    PrintArray(Cnt);
}

BTW, your somewhat odd placement of "using namespace std" makes me wonder if you think it doesn't apply to the headers after it. But it does.

And be sure to use [code] [/code] tags when posting code (like I did).

And delete your duplicate post!
Last edited on
I know it does apply but I'm lazy and it comes to me very easy to write when asking the issue.
Thanks a lot for your suggestions. Sorry for stating the problem wrongly.
Previously I've done what you've suggested.
The output of yours and mine is like the following which does not satisfy me.
As you are seeing there are gap line between the second and third row. I


  1   2   3 
  4   5   6 

  7   8   9 
 10  11  12 
This is my solution.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
template<typename T>
void PrintArray(const T& e) {
    cout << e << " ";
};
template<typename T, size_t N>
void PrintArray(const std::array<T,N>& A) {
    auto next(*A.begin());
    for (auto& e : A) 
        PrintArray(e);
    if (is_integral<decltype(next)>::value) cout << '\n';
};
        
int main() 
{
    TCount2 Cnt = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; 
    std::cout << std::boolalpha;
    PrintArray(Cnt);
}

This is the program output
Start
1 2 3 
4 5 6 
7 8 9 
10 11 12 
0
Finish
sabetaytoros,

1
2
    auto next(*A.begin());
    if (is_integral<decltype(next)>::value) cout << '\n';


can be shorter

if (is_integral<T>::value) cout << '\n';
Topic archived. No new replies allowed.