我在C中有一个大数组(不是c++,如果这有区别的话)。我想初始化相同值的所有成员。
我发誓我曾经知道一个简单的方法。在我的情况下,我可以使用memset(),但没有一种方法来做到这一点,这是建立在C语法?
我在C中有一个大数组(不是c++,如果这有区别的话)。我想初始化相同值的所有成员。
我发誓我曾经知道一个简单的方法。在我的情况下,我可以使用memset(),但没有一种方法来做到这一点,这是建立在C语法?
当前回答
如果数组声明为静态或全局,则所有元素 在数组中已经有默认默认值0。 一些编译器在调试模式中将array的默认值设置为0。 将默认值设置为0很容易: Int数组[10]= {0}; 但是,对于其他值,您必须使用memset()或loop;
例子: int数组[10]; Memset(数组,- 1,10 *sizeof(int));
其他回答
方法一:
int a[5] = {3,3,3,3,3};
形式化初始化技术。
方法二:
int a[100] = {0};
但值得注意的是
int a[10] = {1};
不将所有值初始化为1
这种方法只对0进行初始化
如果你这样做
int a[100];
一些编译器倾向于接受垃圾值,因此它总是首选这样做
int a[1000] = {0};
有一个快速的方法来初始化任何类型的数组与给定的值。它在大型阵列上工作得非常好。算法如下:
初始化数组的第一个元素(通常的方式) 将已设置的部分复制为未设置的部分,每次复制操作都将大小增加一倍
对于1 000 000个数组元素,它比常规循环初始化快4倍(i5, 2核,2.3 GHz, 4GiB内存,64位):
循环运行时间0.004248[秒]
Memfill()运行时间0.001085[秒]
#include <stdio.h>
#include <time.h>
#include <string.h>
#define ARR_SIZE 1000000
void memfill(void *dest, size_t destsize, size_t elemsize) {
char *nextdest = (char *) dest + elemsize;
size_t movesize, donesize = elemsize;
destsize -= elemsize;
while (destsize) {
movesize = (donesize < destsize) ? donesize : destsize;
memcpy(nextdest, dest, movesize);
nextdest += movesize; destsize -= movesize; donesize += movesize;
}
}
int main() {
clock_t timeStart;
double runTime;
int i, a[ARR_SIZE];
timeStart = clock();
for (i = 0; i < ARR_SIZE; i++)
a[i] = 9;
runTime = (double)(clock() - timeStart) / (double)CLOCKS_PER_SEC;
printf("loop runtime %f [seconds]\n",runTime);
timeStart = clock();
a[0] = 10;
memfill(a, sizeof(a), sizeof(a[0]));
runTime = (double)(clock() - timeStart) / (double)CLOCKS_PER_SEC;
printf("memfill() runtime %f [seconds]\n",runTime);
return 0;
}
如果你的编译器是GCC,你可以使用以下“GNU扩展”语法:
int array[1024] = {[0 ... 1023] = 5};
查看详细描述: http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Designated-Inits.html
略带开玩笑的回答;写语句
array = initial_value
用你最喜欢的支持数组的语言(我的是Fortran,但还有很多其他语言),并将它链接到你的C代码。你可能想把它包装成一个外部函数。
我知道最初的问题明确地提到了C而不是c++,但如果你(像我一样)来这里寻找c++数组的解决方案,这里有一个巧妙的技巧:
如果你的编译器支持fold表达式,你可以使用template magic和std::index_sequence生成一个包含你想要的值的初始化列表。你甚至可以把它想象成一个老板:
#include <array>
/// [3]
/// This functions's only purpose is to ignore the index given as the second
/// template argument and to always produce the value passed in.
template<class T, size_t /*ignored*/>
constexpr T identity_func(const T& value) {
return value;
}
/// [2]
/// At this point, we have a list of indices that we can unfold
/// into an initializer list using the `identity_func` above.
template<class T, size_t... Indices>
constexpr std::array<T, sizeof...(Indices)>
make_array_of_impl(const T& value, std::index_sequence<Indices...>) {
return {identity_func<T, Indices>(value)...};
}
/// [1]
/// This is the user-facing function.
/// The template arguments are swapped compared to the order used
/// for std::array, this way we can let the compiler infer the type
/// from the given value but still define it explicitly if we want to.
template<size_t Size, class T>
constexpr std::array<T, Size>
make_array_of(const T& value) {
using Indices = std::make_index_sequence<Size>;
return make_array_of_impl(value, Indices{});
}
// std::array<int, 4>{42, 42, 42, 42}
constexpr auto test_array = make_array_of<4/*, int*/>(42);
static_assert(test_array[0] == 42);
static_assert(test_array[1] == 42);
static_assert(test_array[2] == 42);
static_assert(test_array[3] == 42);
// static_assert(test_array[4] == 42); out of bounds
您可以查看工作中的代码(在Wandbox中)