C++ 布尔数组初始化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39058111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:48:34  来源:igfitidea点击:

C++ boolean array initialization

c++arraysinitializationboolean

提问by False Promise

I want to initilize all elements from my 2-dimensional boolean array to false.

我想将二维布尔数组中的所有元素初始化为 false。

size_t n, m;
cin >> n >> m;
bool arr[n][m] = {false};
for(size_t i = 0; i < n; i++){
    for(size_t j = 0; j < m; j++){
        cout << arr[i][j] << " ";
    }
    cout << endl;
}

But i'm getting very confused with the output. For example, if n = 5 and m = 5, i have the following :

但我对输出感到非常困惑。例如,如果 n = 5 和 m = 5,我有以下内容:

0 27 64 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

So, what's wrong with the code?

那么,代码有什么问题?

采纳答案by Ricardo Ortega Maga?a

Initialize the values with a for structure, exactly as how you print it

使用 for 结构初始化值,与您打印它的方式完全一样

     bool arr[n][m]; 
     for(size_t i = 0; i < n; i++){
         for(size_t j = 0; j < m; j++){
             arr[i][j]=false;
         }
     }

回答by Roby

#include <iostream>

template<int N>
void print(bool x[N][N] )
{
    for(int i=0; i<N; i++)
    {
        for(int j=0; j<N;j++)
            std::cout << x[i][j] << " ";
        std::cout << "\n";
    }
    std::cout << "\n\n";
};

int main()
{
    bool a[10][10];
    bool b[10][10]{};


    print(a);
    print(b);

  return 0;
}

prints:

印刷:

./main 
120 29 96 0 0 0 0 0 131 10 
64 0 0 0 0 0 168 161 188 139 
4 127 0 0 255 255 0 0 1 0 
0 0 0 176 40 152 253 127 0 0 
153 10 64 0 0 0 0 0 2 0 
0 0 0 0 0 0 221 11 64 0 
0 0 0 0 65 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
144 11 64 0 0 0 0 0 160 8 
64 0 0 0 0 0 32 177 40 152 


0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 

回答by Nihhaar

First thing: Your code worked in my gcc compiler. I don't know why you are getting it wrong.

第一件事:你的代码在我的 gcc 编译器中工作。我不知道你为什么会出错。

There is a rule in C saying that all objects of static storage duration, that are not explicitly initialized by the programmer, must be set to zero.

C 中有一条规则是所有静态存储持续时间的对象,没有被程序员显式初始化,必须设置为零。

Static storage duration in sense is:

意义上的静态存储时长为:

An object with static storage duration resides in the same memory address throughout the program's execution.

在整个程序执行过程中,具有静态存储持续时间的对象驻留在相同的内存地址中。

So, for suppose declaring arr[n][m]={-1} sets only first element to '-1' and others to zero by default. Then also its bad programming style to set arr[n][m]=0.

因此,假设声明 arr[n][m]={-1} 仅将第一个元素设置为“-1”,其他元素默认设置为零。然后也是其糟糕的编程风格设置 arr[n][m]=0。

An better alternative is to use cstring's function memset: memset(arr ,samp, sizeof(arr[0][0]) * m * n);to initialize arr to samp. In your case samp=0.

更好的选择是使用cstring的函数memsetmemset(arr ,samp, sizeof(arr[0][0]) * m * n);将 arr 初始化为 samp。在你的情况下 samp=0。

Update:

更新:

As Martin stated it is better not to use memsetwith multidimensional array. An alternative is use std:fill: std::fill(arr[0], arr[0] + m * n, samp);

正如马丁所说,最好不要memset与多维数组一起使用。另一种方法是使用std:fillstd::fill(arr[0], arr[0] + m * n, samp);

This may not be possible in C but possible in C++.

这在 C 中可能是不可能的,但在 C++ 中是可能的。