How to initialize a 2D array with all values same?

后端 未结 6 1617
独厮守ぢ
独厮守ぢ 2021-01-29 10:49

I want to initialize a 2D array with -1 as all the value. I used memset() for this.

#include 
using namespace std;

int dp[100]         


        
相关标签:
6条回答
  • 2021-01-29 11:18

    It seems like you are looking for a way to init a global array before main like described in Call a function before main? Usually I would recommend to avoid compiler specific extension but your code already is compiler specific and it seems ok for you. This is a gcc extension. It can cause strange errors. Other global objects could be uninitialized. Parts of this code probably causes undefined behavior. Just do it if you want to shoot you in your foot.

    #include <bits/stdc++.h>
    using namespace std;
    
    void beforeMain (void) __attribute__((constructor));
    
    int dp[100][100];
    void beforeMain (void) {
        memset(dp, -1, sizeof(dp));
    }
    
    int main() {
        std::cout << dp[0][0] << std::endl;
    }
    
    0 讨论(0)
  • 2021-01-29 11:20

    In practice, in this case, it is possible to write an helping templated function, itself based on simple for-range loops.

    One advantage is that is will work for other types of 2D arrays.

    #include    <iostream>
    
    template <typename T2d, typename T>
    void init2d (T2d &arr, T val) {
        for (auto& row: arr) {
            for (auto& i: row) {
                i = val;
            }
        }       
    }
    
    int main() {
        const int  n = 5, m = 6;
        int a[n][m];
    
        init2d (a, -1);
        std::cout << a[n-1][m-1] << "\n";
    }
    

    If you want to initialize before main, the simplest way is to rely on STL, e.g. on std::vector or std::array, like this, for a 5x6 matrix:

    std::vector<std::vector<int>> b(5, std::vector<int> (6, -1));
    
    0 讨论(0)
  • 2021-01-29 11:25

    You can't use memset in the global scope. It must be written in main scope.

    #include <bits/stdc++.h>
    using namespace std;
    
    int dp[100][100];
    
    int main() {
        memset(dp, -1, sizeof(dp));
        cout<<dp[99][99];
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-29 11:26
    for (auto& v : dp) {
        std::fill(std::begin(v), std::end(v), -1);
    }
    

    Inside function, not global scope.

    0 讨论(0)
  • 2021-01-29 11:29

    There is no way (at least I do not know any) to initialize all the elements of an array with a specific value other than 0.

    You can somehow work around in that you call memset in the course of initializing another variable at file scope:

    int dp[100][100];
    auto x = memset(dp, -1, sizeof(dp));
    
    int main() {
        cout<<dp[99][99];
        return 0;
    }
    

    But note: the order of global variable initialization is not guaranteed; the only thing you can rely on is that the memset will have been executed before function main is entered. But then you could do it as first line in main as well.

    Note further that you need good luck when you want to initialize an array elements of type int, as a single int comprises more than one byte; memset fills the elements at byte level, not at element type level. For example, memset(dp, -2, sizeof(dp)) will not lead to an array filled with -2 then; and even -1 requires the architecture to be two's complement to work.

    0 讨论(0)
  • 2021-01-29 11:32

    As others have said, you can't call memset in the global scope. The most correct and clear vay is probably going to be something like

    #include <iostream>
    using namespace std;
    
    const int D1 = 100;
    const int D2 = 100;
    int dp[D1][D2];
    
    void initializeDp()
    {
        for (int i = 0; i < D1; i++) {
            for (int j = 0; j < D2; j++) {
                dp[i][j] = -1;
            }
        }
    }
    
    int main() {
        initializeDp();
        cout << dp[D1-1][D2-1];
        return 0;
    }
    

    If you count global variables as a good practice, obviously. I personally would use a 2d std::array, so it would contain its size itself

    0 讨论(0)
提交回复
热议问题