Anonymous User
Anonymous User

Reputation: 21

How to initialize a 2d array with -1 globally?

The below is the basic code. I want to make the array globally so that i don't have to call it for every function.Now how do I initialize the 2d array with -1. I tried to use memset(arr,-1,sizeof(arr)) just below line 3, but it didn't worked out, so, can anyone tell me what am I doing wrong??

#include <bits/stdc++.h>
using namespace std;
int arr[10][10];

int func(){
     //this function will be using the global arr
    // if(arr[x][y]!=-1)
          //do something
}

int main(){
//the code
}

Upvotes: 0

Views: 104

Answers (3)

Thomas Matthews
Thomas Matthews

Reputation: 57678

The function memset won't work because memset uses bytes and many integers occupy more than one byte.

IMHO, your best source is to use std::fill.
Example:

std::fill(&arr[0][0], &arr[9][9] + 1, -1);

Otherwise, you can always fall back on the nested loop:

for (int r = 0; r < MAX_ROWS; ++r)
{
    for (int c = 0; c < MAX_COLUMNS; ++c)
    {
        arr[r][c] = -1;
    }
}

Your best bet is to let the compiler optimize the nested loops.

There may be some micro-optimizations that you could employ, but the compiler probably already has them in its tool chest.

Upvotes: 1

Swift - Friday Pie
Swift - Friday Pie

Reputation: 14589

There is no direct way to initialize raw array by values that aren't result of default initialization. One of the reasons is that array cannot be returned from function and cannot be assigned directly from anything that is not a {}-list.

Simplest way (since C++14) is to make it part of class-type with constexpr constructor. In C++111 constructor with non-empty body cannot be constexpr.

#include <iostream>

struct MinusOneArray {
    static constexpr int NX =  10;
    static constexpr int NY =  10;
    int arr[NX][NY];
        
    constexpr MinusOneArray() : arr() {
        for(int i = 0; i < NX; ++i)
            for(int j = 0; j < NY; ++j)
                arr[i][j] = -1;
    }
};

int main()
{
    MinusOneArray a;
    auto &arr = a.arr;
    
    for(auto &line: arr) {
        for(auto val: line)
            std::cout << val << ",";
        std::cout << std::endl;
    }
}

Alternative is to use standard structure std::array and initialize it with constexpr function, how SergeyA offered.

Upvotes: 0

SergeyA
SergeyA

Reputation: 62563

I do not know the good way to initialize a built-in array in place without code repetition. I do, however, know a way to initialize std::array:

#include <array>
#include <utility>
#include <cstddef>

template<size_t... Ix>
auto constexpr make1array(int v, std::index_sequence<Ix...>) {
    auto populate = [](int v, size_t) { return v; };
    std::array<int, 10> a = { populate(v, Ix)... };
    return a;
}
template<size_t... Ix1, size_t... Ix2>
auto constexpr make2array(int v, std::index_sequence<Ix1...> seq, std::index_sequence<Ix2...>) {
    auto populate = [](auto v, size_t)  { return v; };
    std::array<std::array<int, 10>, 10> a = { populate(make1array(v, seq), Ix2)... };
    return a;
}

std::array<std::array<int, 10>, 10> arr = make2array(-1, std::make_index_sequence<10>{}, std::make_index_sequence<10>{});

This code produces an array pre-populated with -1 as the value at compile time.

Upvotes: 1

Related Questions