1

hope you can help me.

Let's say I have two matrix arrays:

int n1[3][3]={{2,7,3},{1,8,7},{5,6,2}};
int n2[3][3]={{4,5,8},{5,5,6},{2,6,2}};

Then I can call one of them using

cout << n1[0][0] << endl;

which will return 2 of course but I want to get something like

cout << n[1][0][0] << endl; // Returns 2
cout << n[2][0][0] << endl; // Returns 4

Can you let me know how can I get this done?

4
  • You've already used 2D array, then why don't you use 3D array? Commented Aug 31, 2014 at 13:08
  • I didn't know something like this exists. Not everyone has years of experience in programming and documentation for C++ is very hard to understand. Also as Nawaz said that raw Arrays are not the best solution. I used std::array in my code. Commented Sep 1, 2014 at 10:52
  • Yes. In C++11, std::array is better than raw array except a bit long code. However, if you decide to use that, I recommend you to change your all raw arrays in your code into std::array for consistency. Commented Sep 1, 2014 at 15:12
  • +1 to compensate unnecessary (or clueless) down vote Commented Sep 1, 2014 at 16:55

3 Answers 3

10

Define n as:

int n[2][3][3]= {
                 {{2,7,3},{1,8,7},{5,6,2}},
                 {{4,5,8},{5,5,6},{2,6,2}}
               };

Note that raw arrays in C++ are not good enough. Better use std::array:

std::array<std::array<std::array<int,3>,3>, 2>  n = {
                 {{2,7,3},{1,8,7},{5,6,2}},
                 {{4,5,8},{5,5,6},{2,6,2}}
               };

Well, that looks ugly — use typedef (or alias) to simplify the syntax:

 template<typename T, std::size_t M, std::size_t N, std::size_t P>
 using array3d = std::array<std::array<std::array<T,P>,N>, M>;

Then use it as:

array3d<int,2,3,3>  n = {
                          {{2,7,3},{1,8,7},{5,6,2}},
                          {{4,5,8},{5,5,6},{2,6,2}}
                        };

Thats better.


You could generalize the alias as:

#include <array> 

template<typename T, std::size_t D, std::size_t ... Ds> 
struct make_multi_array 
  : make_multi_array<typename make_multi_array<T,Ds...>::type, D> {}; 

template<typename T, std::size_t D>
struct make_multi_array<T,D> { using type = std::array<T, D>;  };

template<typename T, std::size_t D, std::size_t  ... Ds> 
using multi_array = typename make_multi_array<T,D,Ds...>::type;

Then use it as (read the comments for better understanding):

 //same as std::array<int,10> 
 //similar to int x[10] 
 multi_array<int,10>   x;   

 //same as std::array<std::array<int,20>,10>
 //similar to int y[10][20] 
 multi_array<int,10,20> y;   

 //same as std::array<std::array<std::array<int,30>,20>,10>
 //similar to int z[10][20][30]
 multi_array<int,10,20,30> z; 

Hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

1

Then you need to create a three-dimensional array and access the second row, first column, and first depth-column.

Comments

1
int foo[x][y][z] = {};

This makes a multidimensional array of size x*y*z with all elements initialized to 0.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.