简体   繁体   English

按行,列和深度顺序打印多维矢量

[英]printing multidimensional vector in rows , cols and depth order

I am trying to print 3D array of vectors in order of rows x cols x depth, but getting something different. 我试图按行x cols x深度的顺序打印向量的3D数组,但是却有所不同。 eg I want to print a 3x2x5 array of vectors, the output of my code is : 例如,我想打印一个3x2x5的向量数组,我的代码输出是:

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. 输出如下:2(行)x 5(cols)x 3,对我来说似乎不对。

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 << " ";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM