1
我有一个设置元素1 - 10的数组。我已经决定了数组的大小,我决定了数组的元素。我的问题是,如何创建一个大小为x的数组,并用元素1,2,3,4填充它。 。 。如何更改数组的大小并使用自动化元素填充它?
//sets values of the array elements and print them.
cout << "Array should contain x integers set to 1,2,3" << endl;
// QUESTION: How can I change the size of the array and have
// have values automatically entered?
int array[] = { 1,2,3,4,5,6,7,8,9,10 };
for (int i = 0; i <= (sizeof(array)/sizeof(int)-1); ++i) {
// sizeof(array)/sizeof(int) = 36/4. ints are 4.
cout << "Element " << i << " = " << array[i] << endl;
}
cout << "The number of elements in the array is: "
<< sizeof(array)/sizeof(int) << endl;
cout << endl;
cout << endl;
你需要一个动态数组,对吧?我认为这个http://stackoverflow.com/questions/4029870/how-to-create-a-dynamic-array-of-integers是有用的。 –
'int array [x]; std :: iota(std :: begin(array),std :: end(array),1);' –
编译时是否已知数组的大小? – zett42