How to define a 3d array with each dimension a different type in C++?
-
I want to define a 3D array like this:
Type ary[3/*enumeration type*/][6/*int*/][7/*const wchar**/];
Is it possible in C++? I'm using Visual Studio 2010, Boost library is not allowed. If it's possible, please tell me how to initialize each dimension?
-
I want to define a 3D array like this:
Type ary[3/*enumeration type*/][6/*int*/][7/*const wchar**/];
Is it possible in C++? I'm using Visual Studio 2010, Boost library is not allowed. If it's possible, please tell me how to initialize each dimension?
You cannot do that since an array must be typed at its declaration, and that defines the type of each element in the array. You could declare an array of
unsigned int
and then use casts to set different types in some cells, but it would still be somewhat confusing. Perhaps you should explain what problem you are trying to solve.Veni, vidi, abiit domum
-
You cannot do that since an array must be typed at its declaration, and that defines the type of each element in the array. You could declare an array of
unsigned int
and then use casts to set different types in some cells, but it would still be somewhat confusing. Perhaps you should explain what problem you are trying to solve.Veni, vidi, abiit domum
+5... when you need to support multiple types, use the largest (byte-wise) and cast... but yes, it may be a bit confusing to others reading the code
-
+5... when you need to support multiple types, use the largest (byte-wise) and cast... but yes, it may be a bit confusing to others reading the code
-
I want to define a 3D array like this:
Type ary[3/*enumeration type*/][6/*int*/][7/*const wchar**/];
Is it possible in C++? I'm using Visual Studio 2010, Boost library is not allowed. If it's possible, please tell me how to initialize each dimension?
-
I want to define a 3D array like this:
Type ary[3/*enumeration type*/][6/*int*/][7/*const wchar**/];
Is it possible in C++? I'm using Visual Studio 2010, Boost library is not allowed. If it's possible, please tell me how to initialize each dimension?
If you mean that the type of the index value needs to be different, then the only way to achieve this is defining your own subscript operator overloads, and that means you need to define your own array class(es). Here's an example of how to define and use such overloads to create a 3D array like you describe:
enum EMyColors {
C_RED,
C_GREEN,
C_YELLOW
};
template <class Base, int n_elements>
class CMyArray1 {
std::vector<Base> values;
public:
CMyArray1() : values(n_elements) {} // initialize array of given size
Base& operator[](const wchar_t wc) {
size_t index = wc - L'a'; // 'a'-based index (or whatever...)
assert(index < values.size());
return values[index];
}
};
template <class Base, int n_vectors, int n_elements>
class CMyArray2 {
std::vector< CMyArray1<Base, n_elements> > vectors;
public:
CMyArray2() : vectors(n_vectors) {} // initialize array of given size
CMyArray1<Base, n_elements>& operator[](int i) {
size_t index = i - 1; // 1-based index (or whatever...)
assert(index < vectors.size());
return vectors[index];
}
};
template <class Base, int n_arrays, int n_vectors, int n_elements>
class CMyArray3 {
std::vector< CMyArray2<Base, n_vectors, n_elements> > arrays;
public:
CMyArray3() : arrays(n_arrays) {} // initialize array of given size
CMyArray2<Base, n_vectors, n_elements>& operator[](EMyColors c) {
size_t index;
switch (c) {
case C_RED:
index = 0;
break;
case C_GREEN:
index = 1;
break;
default:
index = 2;
break;
}
return arrays[index];
}
};
int foo() {
CMyArray3<int, 3, 5, 8> my_array;
my_array[C_GREEN][3][L'c'] = 17;
}GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
You cannot do that since an array must be typed at its declaration, and that defines the type of each element in the array. You could declare an array of
unsigned int
and then use casts to set different types in some cells, but it would still be somewhat confusing. Perhaps you should explain what problem you are trying to solve.Veni, vidi, abiit domum
You seem to understand the question as storing elements of different types, but I think the request is about using alternate types of index values. See my response.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
I want to define a 3D array like this:
Type ary[3/*enumeration type*/][6/*int*/][7/*const wchar**/];
Is it possible in C++? I'm using Visual Studio 2010, Boost library is not allowed. If it's possible, please tell me how to initialize each dimension?
char *** array; and allocate it.
-
You seem to understand the question as storing elements of different types, but I think the request is about using alternate types of index values. See my response.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
I want to define a 3D array like this:
Type ary[3/*enumeration type*/][6/*int*/][7/*const wchar**/];
Is it possible in C++? I'm using Visual Studio 2010, Boost library is not allowed. If it's possible, please tell me how to initialize each dimension?
Simply define the needed datatypes as UNION and make your array of the union type:
enum MyEnum { X, Y, Z };
union MyUnion {
MyEnum _Enum;
int _Int;
const wchar *_Pwchar;
};MyUnion ary[3][6][7];
-
If you mean that the type of the index value needs to be different, then the only way to achieve this is defining your own subscript operator overloads, and that means you need to define your own array class(es). Here's an example of how to define and use such overloads to create a 3D array like you describe:
enum EMyColors {
C_RED,
C_GREEN,
C_YELLOW
};
template <class Base, int n_elements>
class CMyArray1 {
std::vector<Base> values;
public:
CMyArray1() : values(n_elements) {} // initialize array of given size
Base& operator[](const wchar_t wc) {
size_t index = wc - L'a'; // 'a'-based index (or whatever...)
assert(index < values.size());
return values[index];
}
};
template <class Base, int n_vectors, int n_elements>
class CMyArray2 {
std::vector< CMyArray1<Base, n_elements> > vectors;
public:
CMyArray2() : vectors(n_vectors) {} // initialize array of given size
CMyArray1<Base, n_elements>& operator[](int i) {
size_t index = i - 1; // 1-based index (or whatever...)
assert(index < vectors.size());
return vectors[index];
}
};
template <class Base, int n_arrays, int n_vectors, int n_elements>
class CMyArray3 {
std::vector< CMyArray2<Base, n_vectors, n_elements> > arrays;
public:
CMyArray3() : arrays(n_arrays) {} // initialize array of given size
CMyArray2<Base, n_vectors, n_elements>& operator[](EMyColors c) {
size_t index;
switch (c) {
case C_RED:
index = 0;
break;
case C_GREEN:
index = 1;
break;
default:
index = 2;
break;
}
return arrays[index];
}
};
int foo() {
CMyArray3<int, 3, 5, 8> my_array;
my_array[C_GREEN][3][L'c'] = 17;
}GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
An excellent and creative solution!!
The difficult we do right away... ...the impossible takes slightly longer.