简体   繁体   English

c++ 传二维char数组

[英]c++ pass 2-dimensional char array

I am working on the CSES Problem Set for practice on C++.我正在研究 CSES 问题集,以便在 C++ 上进行练习。 I am working on Windows 10 Pro 64-bit using the g++ compiler from RTools -- g++ (Built by Jeroen for the R-project) 8.3.0 -- and Visual Studio Code.我正在使用来自 RTools 的 g++ 编译器 - g++ (由 Jeroen 构建 - 和 R 项目代码)开发 Windows 10 Pro 64 位。 I am compiling all of the programs that I have completed using the following:我正在使用以下代码编译我已完成的所有程序:

    g++ -std=c++17 -Wall .\filename.cpp -o .\filename.exe  

I have solved this problem using DFS, but once I saw another user's solution I realized that my solution combined (1) DFS and (2) finding a valid edge, so I was trying to implement a solution that separated (1) and (2).我已经使用 DFS 解决了这个问题,但是当我看到另一个用户的解决方案时,我意识到我的解决方案结合了 (1) DFS 和 (2) 找到一个有效的边缘,所以我试图实现一个分离 (1) 和 (2) 的解决方案)。 I believe my problems center on forgetting how to pass a 2-d char array to a function.我相信我的问题集中在忘记如何将二维字符数组传递给 function。 In the code (below), I have included the errors I am receiving from VSCode IntelliSense using the '^' symbol where these errors occur (3 errors total).在代码(如下)中,我使用了发生这些错误的“^”符号包含了从 VSCode IntelliSense 收到的错误(总共 3 个错误)。 I have formatted the IntelliSense for each error by separating the error line to two (2) lines.我通过将错误行分隔为两 (2) 行来格式化每个错误的 IntelliSense。 Also, I re-formatted my code to fit in 80-column width (for clarity posting here):另外,我重新格式化了我的代码以适应 80 列宽(为了清楚起见,请在此处发布):

    // 
    // CSES Problem Set 
    // Projects 
    // 
    // From: 
    //       https://cses.fi/problemset/task/1192 
    // 
    // Example: 
    // Input: 
    //          5 8 
    //          ######## 
    //          #..#...# 
    //          ####.#.# 
    //          #..#...# 
    //          ######## 
    // 
    // Output: 
    //          3 
    // 
    #include <iostream>
    
    using namespace std; 
    
    bool valid_edge( int y, int x, int MAX_ROWS, int MAX_COLS, char bld_map ) { 
        return ( y >= 0 && y < MAX_ROWS && 
                 x >= 0 && x < MAX_COLS && bld_map[ y ][ x ] == '.' ); 
                                                    ^
                                                    expression must have 
                                                    pointer-to-object type 
    } // bool valid 
    
    void dfs( int y, int x, int MAX_ROWS, int MAX_COLS, char bld_map ) { 
        bld_map[ y ][ x ] = '#'; 
                 ^
                 expression must have 
                 pointer-to-object type 
        if( valid_edge( y - 1, x, MAX_ROWS, MAX_COLS, bld_map ) ) { 
            dfs( y - 1, x, MAX_ROWS, MAX_COLS, bld_map ); 
        } // if 
        if( valid_edge( y + 1, x, MAX_ROWS, MAX_COLS, bld_map ) ) { 
            dfs( y + 1, x, MAX_ROWS, MAX_COLS, bld_map ); 
        } // if 
        if( valid_edge( y, x - 1, MAX_ROWS, MAX_COLS, bld_map ) ) { 
            dfs( y, x - 1, MAX_ROWS, MAX_COLS, bld_map ); 
        } // if 
        if( valid_edge( y, x + 1, MAX_ROWS, MAX_COLS, bld_map ) ) { 
            dfs( y, x + 1, MAX_ROWS, MAX_COLS, bld_map ); 
        } // if 
    
    } // void dfs 
    
    int main()
    {
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
    
        int bld_rows; // rows 
        int bld_cols; // columns 
    
        cin >> bld_rows >> bld_cols; 
    
        char bld_map[ 1000 ][ 1000 ]; 
    
        for( int i = 0; i < bld_rows; i++ ) { 
            for( int j = 0; j < bld_cols; j++ ) { 
                cin >> bld_map[ i ][ j ]; 
            } // for 
        } // for 
    
        int room_count = 0; 
    
        for( int y = 0; y < bld_rows; y++ ) { 
            for( int x = 0; x < bld_cols; x++ ) { 
                if( valid_edge( y, x ) ) { 
                    dfs( y, x, bld_rows, bld_cols, bld_map ); 
                                                   ^ 
                                                   arg of type "char (*)[1000]" 
                                                   is incompatible w/ parameter 
                                                   of type "char"
                    room_count++; 
                } // if 
            } // for 
        } // for 
    
        cout << room_count; 
    
        return 0; 
    
    } // int main 

I have looked at other StackOverflow Q & A, but none of the answers clarified the process of properly passing a 2-d char array to a function.我查看了其他 StackOverflow 问答,但没有一个答案阐明了将二维字符数组正确传递给 function 的过程。 Does anyone know how to correct my code?有谁知道如何更正我的代码? If anyone would like to see my (different) working version using the C++ STL, I have a copy on CodePile:如果有人想使用 C++ STL 查看我的(不同)工作版本,我在 CodePile 上有一份副本:

https://www.codepile.net/pile/1EPZQMze

Notwithstanding the error in if( valid_edge( y, x ) ) , your culprit is the the syntax with which you use to pass the 2D array to the function.尽管if( valid_edge( y, x ) )中存在错误,但罪魁祸首是用于将二维数组传递给 function 的语法。 In void dfs( int y, int x, int MAX_ROWS, int MAX_COLS, char bld_map ) as well as in bool valid_edge( int y, int x, int MAX_ROWS, int MAX_COLS, char bld_map ) you are passing a char called bld_map to the function, and then do bld_map[ y ][ x ] .void dfs( int y, int x, int MAX_ROWS, int MAX_COLS, char bld_map )以及bool valid_edge( int y, int x, int MAX_ROWS, int MAX_COLS, char bld_map )中,您将一个名为bld_mapchar传递给function,然后做bld_map[ y ][ x ] You can't do that to a char.你不能对一个字符这样做。

You could change the parameter to char bld_map[][1000] (or whatever the size of your array is), or just use the built-in std::array which is preferable to working with C-style arrays in C++.您可以将参数更改为char bld_map[][1000] (或任何数组的大小),或者只使用内置的 std::array,这比在 C++ 中使用 C 样式 arrays 更可取。

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

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