用C初始化数组下标具体项目++ [英] Initialize specific subscript item in array by c++
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/9048883/static-array-initialization-of-individual-elements-in-c\">Static在C ++中各个元素的数组初始化
在C,我可以在阵列像这样初始化具体项目标:
In C, I can initialize specific subscript item in array like this:
int array[100] = {[22] = {1}, [33] = {33}};
但这个片段不能用g ++编译。
所以我怎么能由C ++中的数组初始化特定标项目。
but this snippet can not compile by g++. so how can I initialize specific subscript item in array by c++.
推荐答案
下标初始化只允许在C和不是C ++。你可以在这里<一个检查的详细文档href=\"http://publib.boulder.ibm.com/infocenter/compbgpl/v9v111/index.jsp?topic=/com.ibm.xlcpp9.bg.doc/language_ref/aryin.htm\" rel=\"nofollow\">http://publib.boulder.ibm.com/infocenter/compbgpl/v9v111/index.jsp?topic=/com.ibm.xlcpp9.bg.doc/language_ref/aryin.htm
The subscript initialization is only allowed in C and not in C++. You can check the detailed documentation here http://publib.boulder.ibm.com/infocenter/compbgpl/v9v111/index.jsp?topic=/com.ibm.xlcpp9.bg.doc/language_ref/aryin.htm
如果您要初始化每个元素,你可以声明数组后,并在以下行初始化值做到这一点;
If you want to initialize each element, you can do it after declaring the array and in the following lines initializing the values;
#include<iostream>
using namespace std;
int main(){
int arr[100];
arr[22]=33;
arr[33]=66;
cout<<arr[22]<<"\t"<<arr[33];
return 0;
}
它产生的输出如下:
It produces the following output:
$ ./a.exe
33 66
这篇关于用C初始化数组下标具体项目++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!