3d arrays

Jan 27, 2014 at 3:57pm
Can we create 3d arrays in c++ programs?
Jan 27, 2014 at 4:10pm
You can create an array of any element type.

That element type can be another array type, or an array of arrays, or an array of arrays of arrays, or... etc.

So you can create arrays of any number of dimensions.
Jan 27, 2014 at 4:24pm
can we print them in 3d format?
Last edited on Jan 27, 2014 at 4:24pm
Jan 27, 2014 at 4:30pm
What is "3d format"?
Jan 27, 2014 at 5:20pm
by "3d format" i mean one 2d array infront/behind another 2d array.
Jan 27, 2014 at 5:27pm
So, you mean some kind of graphical display? There's nothing at all that would make that impossible to write, using some kind of UI/graphics library.

I don't know of any ready-made libraries that do that already, but it's possible someone else has written something.
Last edited on Jan 27, 2014 at 5:27pm
Jan 27, 2014 at 5:42pm
yeah, exactly i meant graphical display.
Jan 27, 2014 at 6:20pm
> yeah, exactly i meant graphical display.

Do you really need a graphical display to present a 3d array in an understandable form?
For instance, is an output which looks something like this all that hard to understand?

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
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <iomanip>
#include <string>
#include <numeric>
#include <iterator>

template < typename T, std::size_t A, std::size_t B, std::size_t C >
using array_3d = T[A][B][C] ;

template < typename T, std::size_t A, std::size_t B, std::size_t C >
std::ostream& operator<< ( std::ostream& stm, const array_3d<T,A,B,C>& a3d )
{
    const int width = stm.width() ;
    const char fill = stm.fill() ;
    int spacer = width ;

    for( const auto& a2d : a3d )
    {
        for( const auto& row : a2d )
        {
            std::cout << std::string( spacer, ' ' ) ;
            for( const T& v : row ) 
                stm << std::setw(width) << std::setfill(fill) << v << ' ' ;
            stm << '\n' ;
        }
        std::cout << '\n' ;
        spacer += width + 1 ;
    }

    return stm ;
}

int main()
{
    int a[3][4][5] ;
    std::iota( std::begin( a[0][0] ), std::end( a[2][3] ), 0 ) ;

    std::cout << "the array contains:\n\n" << std::setw(3) << a << '\n' ;
}























the array contains:

     0   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  29 
        30  31  32  33  34 
        35  36  37  38  39 

            40  41  42  43  44 
            45  46  47  48  49 
            50  51  52  53  54 
            55  56  57  58  59

http://coliru.stacked-crooked.com/a/44589e287378fe39
Jan 27, 2014 at 6:23pm
@JLBorges
as for now and for me, it doesn't matter if output is understandable. i'm just experimenting with c++
Topic archived. No new replies allowed.