How to initialize a static array to certain value in a function in c++?

后端 未结 4 679
傲寒
傲寒 2021-01-20 20:50

I am trying to init a static array in a function.

int query(int x, int y) {
    static int res[100][100]; // need to          


        
相关标签:
4条回答
  • 2021-01-20 21:33

    First of all, I strongly recommend moving from C arrays to std::array. If you do this you can have a function to perform the initialization (otherwise you can't, as a function cannot return C arrays):

    constexpr std::array<std::array<int, 100>, 100> init_query_array()
    {
        std::array<std::array<int, 100>, 100> r{};
        for (auto& line : r)
            for (auto& e : line)
                e = -1;
        return r;
    }
    
    int query(int x, int y) {
        static std::array<std::array<int, 100>, 100> res = init_query_array();
    
        if (res[x][y] == -1) {
            res[x][y] = time_consuming_work(x, y);
        }
        return res[x][y];
    }
    

    Another option, that I actually like more is to perform the init in a lambda:

    int query(int x, int y) {
        static auto res = [] {
            std::array<std::array<int, 100>, 100> r;
            for (auto& line : r)
                for (auto& e : line)
                    e = -1;
            return r;
        }();
    
        if (res[x][y] == -1) {
            res[x][y] = time_consuming_work(x, y);
        }
        return res[x][y];
    }
    
    0 讨论(0)
  • 2021-01-20 21:35

    You can do it for example the following way by means of introducing one more static variable

    int query(int x, int y) {
        static bool initialized;
        static int res[100][100]; // need to be initialized to -1
    
        if ( not initialized )
        {
            for ( auto &row : res )
            {
                for ( auto &item : row ) item = -1;
            }
    
            initialized = true;
        }        
    
        if (res[x][y] == -1) {
            res[x][y] = time_consuming_work(x, y);
        }
        return res[x][y];
    }
    
    0 讨论(0)
  • 2021-01-20 21:36

    You can use fill with std::array and a IIL(immediately invoked lambda) :

    static std::array<std::array<int, 100>, 100> res = [] () {
         std::array<int, 100> default_arr;
         default_arr.fill(-1);
    
         std::array<std::array<int, 100>, 100> ret;
         ret.fill(default_arr);
    
         return ret;
    }();
    
    0 讨论(0)
  • 2021-01-20 21:42

    You can't do this. You need an explicit for loop and a flag to avoid initializing more than once:

    int query(int x, int y) {
        static bool initilized = false;
        static int res[100][100]; // need to be initialized to -1
        if (!initilized) {
            initilized = true;
            for (int i = 0; i != 100; ++i) {
                for (int j = 0; j != 100; ++j) {
                    res[i][j] = -1;
                }
            }
        }
        if (res[x][y] == -1) {
            res[x][y] = time_consuming_work(x, y);
        }
        return res[x][y];
    }
    
    0 讨论(0)
提交回复
热议问题