C ++中各个元素的静态数组初始化 [英] Static array initialization of individual elements in C++

查看:221
本文介绍了C ++中各个元素的静态数组初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码适用于GCC的C编译器,但不适用于C ++编译器。是否有一个捷径在C ++中实现相同的结果?

The following code works with GCC's C compiler, but not with the C++ compiler. Is there a "shortcut" to achieve the same result in C++?

int array[10] = {
    [1] = 1,
    [2] = 2,
    [9] = 9
};

编辑:
Humm,我发现这一点,澄清一切。
http://eli.thegreenplace.net/2011/02/15/array-initialization-with-enum-indices-in-c-but-not-c/

推荐答案

这种初始化形式只在C99标准中定义。它不会适用于C ++。所以,你必须一个一个地分配你的元素:

This form of initialization is only defined in the C99 standard. It does not apply to C++. So, you'll have to assign your elements one-by-one:

int array[10] = { 0 };
array[1] = 1;
array[2] = 2;
array[9] = 9;

这篇关于C ++中各个元素的静态数组初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆