簡體   English   中英

按行,列和深度順序打印多維矢量

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

我試圖按行x cols x深度的順序打印向量的3D數組,但是卻有所不同。 例如,我想打印一個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

輸出如下:2(行)x 5(cols)x 3,對我來說似乎不對。

這是我的代碼...

#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;

}

您幾乎可以正常工作了。 我發現代碼中唯一缺少的是一行中的數字之間的空格。

std::cout << C[ix][iy][iq] << " ";
                           ^^^^^^

即使在該行的最后一個數字之后,也將添加一個空格。 如果這是不可接受的,則需要一些其他邏輯:

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