Static array initialization of individual elements in C++(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
};
嗯,我发现了这一点,澄清了一切.http://eli.thegreenplace.net/2011/02/15/array-initialization-with-enum-indices-in-c-but-not-c/
Humm, I found this, clarifies everything. 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++中单个元素的静态数组初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++中单个元素的静态数组初始化
基础教程推荐
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01