如何在以下数组中使用fill_n() ?

时间:2021-11-04 23:40:37

I have this array

我有这个数组

 unsigned char        bit_table_[10][100];

What is the right way to fill it with 0. I tried

用0填充的正确方法是什么?我试着

std::fill_n(bit_table_,sizeof(bit_table_),0x00);

but vc 2010 flags it as error.

但是vc 2010认为这是错误的。

3 个解决方案

#1


3  

On initialization:

初始化:

unsigned char bit_table_[10][100] = {};

If it's a class member, you can initialize it in the constructor, like this:

如果它是类成员,可以在构造函数中初始化它,如下所示:

MyClass::MyClass()
    :bit_table_()
{}

Otherwise:

否则:

std::fill_n(*bit_table_,sizeof(bit_table_),0);

#2


2  

The type of bit_table_ is unsigned char [10][100], which will decay (that is, the compiler allows it to be implicitly converted to) into unsigned char (*)[100], that is, a pointer to an array of 100 unsigned chars.

bit_table_的类型是无符号char[10][100],它将衰减(即编译器允许它隐式地转换为)到无符号char(*)[100],即指向100个无符号字符的数组的指针。

std::fill_n(bit_table_, ...) is then instantiated as: std::fill_n(unsigned char (*)[100], ...) which means it expects a value of type unsigned char [100] to initialize bit_table_ with. 0 is not convertible to that type, so the compilation fails.

std::fill_n(bit_table_,…),然后被实例化为:std::fill_n(unsigned char(*)[100],…),这意味着它期望一个无符号char[100]的值来初始化bit_table_ with。0不能转换为该类型,因此编译失败。

Another way to think about it is that the STL functions that deal with iterators only deal with a single dimension. If you are passing in a multidimensional structure those STL functions will only deal with a single dimension.

另一种考虑方法是处理迭代器的STL函数只处理一个维度。如果你传递一个多维结构,那些STL函数只会处理一个维度。

Ultimately, you can't do this; there is no way to assign to an array type. I.e., since you can't do this:

最终,你不能这么做;没有办法分配给数组类型。即。,因为你不能这样做:

char table[100];
char another_table[100]= { };
table= another_table;

you can't use std::fill_n on multidimensional arrays.

在多维数组中不能使用std::fill_n。

#3


0  

You can also try unsigned char bit_table_[10][100]= { 0 } to fill it with zeros.

您还可以尝试使用unsigned char bit_table_[10][100]={0}来填充0。

int main()
{
    unsigned char        bit_table_[10][100]= { 0 };
    return 0;

}

#1


3  

On initialization:

初始化:

unsigned char bit_table_[10][100] = {};

If it's a class member, you can initialize it in the constructor, like this:

如果它是类成员,可以在构造函数中初始化它,如下所示:

MyClass::MyClass()
    :bit_table_()
{}

Otherwise:

否则:

std::fill_n(*bit_table_,sizeof(bit_table_),0);

#2


2  

The type of bit_table_ is unsigned char [10][100], which will decay (that is, the compiler allows it to be implicitly converted to) into unsigned char (*)[100], that is, a pointer to an array of 100 unsigned chars.

bit_table_的类型是无符号char[10][100],它将衰减(即编译器允许它隐式地转换为)到无符号char(*)[100],即指向100个无符号字符的数组的指针。

std::fill_n(bit_table_, ...) is then instantiated as: std::fill_n(unsigned char (*)[100], ...) which means it expects a value of type unsigned char [100] to initialize bit_table_ with. 0 is not convertible to that type, so the compilation fails.

std::fill_n(bit_table_,…),然后被实例化为:std::fill_n(unsigned char(*)[100],…),这意味着它期望一个无符号char[100]的值来初始化bit_table_ with。0不能转换为该类型,因此编译失败。

Another way to think about it is that the STL functions that deal with iterators only deal with a single dimension. If you are passing in a multidimensional structure those STL functions will only deal with a single dimension.

另一种考虑方法是处理迭代器的STL函数只处理一个维度。如果你传递一个多维结构,那些STL函数只会处理一个维度。

Ultimately, you can't do this; there is no way to assign to an array type. I.e., since you can't do this:

最终,你不能这么做;没有办法分配给数组类型。即。,因为你不能这样做:

char table[100];
char another_table[100]= { };
table= another_table;

you can't use std::fill_n on multidimensional arrays.

在多维数组中不能使用std::fill_n。

#3


0  

You can also try unsigned char bit_table_[10][100]= { 0 } to fill it with zeros.

您还可以尝试使用unsigned char bit_table_[10][100]={0}来填充0。

int main()
{
    unsigned char        bit_table_[10][100]= { 0 };
    return 0;

}