是否可以使用std :: fill与2d C风格的数组? [英] Is it possible to use std::fill with a 2d C-style array?
问题描述
假设我有: unsigned char my2darray [A] [B];
使用 std :: fill
将2d数组的所有值设置为非0值?
可以使用向量,但这是性能关键代码,我不能承受动态分配。
目前在代码中使用的是 memset(my2darray,0x12,sizeof(my2darray)); / code>但是我想用
std :: fill
替换所有 memset
是的,因为2d数组仍然是被分配为连续内存块的一系列元素。
std :: fill(
& my2darray [0] [0],
& my2darray [0] [0] + sizeof(my2darray)/ sizeof(my2darray [0] [0]),
none_zero_value);
Suppose that I have: unsigned char my2darray[A][B];
Is it possible to use std::fill
to set all values of the 2d array to a non-0 value?
I know a vector can be used but this is performance critical code and I can't afford dynamic allocations. Unfortunately, no C++11 is allowed.
Currently used in the code is memset(my2darray, 0x12, sizeof(my2darray));
but I want to replace all memset
with std::fill
if possible, as the type of the array could become a class in future.
Answers which work for any type, not just chars, appreciated.
Yes, because a 2d array is still a sequence of elements that are allocated as a contiguous block of memory.
std::fill (
&my2darray[0][0],
&my2darray[0][0] + sizeof(my2darray) / sizeof(my2darray[0][0]),
none_zero_value);
这篇关于是否可以使用std :: fill与2d C风格的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!