繁体   English   中英

如何检查 3d 数组的元素是否为空?

[英]How to check if an element of a 3d array is empty?

我有一个填充的 3d 数组和一个 function 打印每个二维数组的选定行但是如果选定的行(从第一个数组开始)为空,它应该打印“行为空”,我不知道如何检查是否它是空的。 我尝试了以下方法:

void printrow(int X[][4][5], int row){
    if(X[0][row][0]==0){
        cout <<"row empty";
    }
    else{
        for(int i=0; i < 4; i++){
            for(int j=0; j < 5;j++){
                cout << X[i][row][j] << " ";
             }
        }
        cout << endl;  
    }
}

也许我应该使用指针? 如果是,该怎么做?

您可以使用std::optional

void printrow(std::array<std::array<std::array<std::optional<int>, 3>, 4>, 5> X, int row){
    if(!X[0][row][0].has_value()){
        cout <<"row empty";
    }
    else{
        for(int i=0; i < 4; i++){
            for(int j=0; j < 5;j++){
                cout << X[i][row][j].value() << " ";
             }
        }
        cout << endl;  
    }
}

暂无
暂无

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

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