A simple program to demonstrate how to declare and use three dimensional arrays in C++ programming language.
I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me at the following email address for further details. If you want to advertise on my website kindly contact me also in my email address also. Thank you.
My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.
My mobile number here in the Philippines is 09173084360.
My telephone number at home here in Bacolod City, Negros Occidental Philippines is +63 (034) 4335675.
Here in Bacolod City I also accepting computer repair, networking, and Arduino Project development at a very affordable price. My website is www.jakerpomperada.blogspot.com and www.jakerpomperada.com
If you like this video please click the LIKE button, SHARE, and SUBSCRIBE to my channel.
Your support on my channel is highly appreciated.
Thank you very much.
Program Listing
#include <iostream>
#include <vector>
#include <cassert>
template<class T>
class Array3D
{
std::vector<T> data;
size_t m_dim1, m_dim2, m_dim3;
public:
Array3D(size_t dim1, size_t dim2, size_t dim3):
m_dim1(dim1), m_dim2(dim2), m_dim3(dim3)
{
assert(m_dim1 > 1 && m_dim2 > 1 && m_dim3 > 1);
data.resize(m_dim1 * m_dim2 * m_dim3);
}
T& operator()(size_t d1, size_t d2, size_t d3)
{
assert(d1 < m_dim1 && d2 < m_dim2 && d3 < m_dim3);
return data[d1*d2*m_dim3 + d2*m_dim3 + d3];
}
const T& operator()(size_t d1, size_t d2, size_t d3) const
{
assert(d1 < m_dim1 && d2 < m_dim2 && d3 < m_dim3);
return data[d1*d2*m_dim3 + d2*m_dim3 + d3];
}
size_t dim1() {return m_dim1;}
size_t dim2() {return m_dim2;}
size_t dim3() {return m_dim3;}
};
int main()
{
Array3D<int> array3d(2,2,2);
for ( size_t d1=0; d1 < 2; ++d1 )
{
for ( size_t d2=0; d2 < 2; ++d2 )
{
for ( size_t d3=0; d3 < 2; ++d3 )
{
array3d(d1,d2,d3) = 3;
}
}
}
array3d(1,1,1) = -99;
std::cout << "Three Dimensional Arrays in C++";
std::cout << "\n\n";
for ( size_t d1=0; d1 < 2; ++d1 )
{
for ( size_t d2=0; d2 < 2; ++d2 )
{
for ( size_t d3=0; d3 < 2; ++d3 )
std::cout << "array3d["<< d1 << "][" << d2 << "][" << d3 << "] = " << array3d(d1, d2, d3) << "\n";
}
}
}