I am trying to print 3D array of vectors in order of rows x cols x depth, but getting something different. eg I want to print a 3x2x5 array of vectors, the output of my code is :
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
The output is like: 2(rows) x 5(cols) x 3, it look wrong to me.
Here is my code...
#include <iostream>
#include <string>
#include <vector>
const int N = 3;
const int M = 2;
const int Q = 5;
typedef std::vector<double> dim1;
typedef std::vector<dim1> array2D;
typedef std::vector<array2D> array3D;
array2D A(N, dim1(M));
array2D B(N, dim1(M));
array3D C(N, array2D(M, dim1(Q)));
int main() {
for (int ix = 0; ix < N; ++ix) {
for (int iy = 0; iy < M; ++iy) {
for (int iq = 0; iq < Q; ++iq) {
C[ix][iy][iq] = 1.0;
}
}
}
for (int ix = 0; ix < N; ++ix) {
for (int iy = 0; iy < M; ++iy) {
for (int iq = 0; iq < Q; ++iq) {
std::cout << C[ix][iy][iq];
}
std::cout << std::endl;
}
std::cout << std::endl;
}
return 0;
}
You have it almost working. The only thing that I see that is missing from your code is a space between the numbers in a row.
std::cout << C[ix][iy][iq] << " ";
^^^^^^
That would add a space even after the last number of the row. If that is not acceptable, you'll need a bit of additional logic:
for (int iq = 0; iq < Q; ++iq) {
std::cout << C[ix][iy][iq];
if ( iq < Q-1 ) {
std::cout << " ";
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.