为什么输出不同,这段代码有什么问题?
why the output is different and what is the wrong in this code?
所以我一直在尝试获取数组大小及其元素的输入,然后将这些元素显示到屏幕上,但是当我举个例子
数组大小:7
数组的元素:1 2 3 4 5 6 7
输出是:
1
2
3
4
5
6
6
代码:
#include <iostream>
using namespace std;
int main () {
int n , Arr[n];
cout << "please put the size of the array " ;
cin >> n;
cout << "please enter array's elemets ";
for (int k=0; k<n ; k++) {
cin >> Arr[k];
}
for (int i=0;i<n;i++){
cout << Arr[i] << endl;
}
}
int Arr[n]
其中 n
不是编译时间常量是非法的 C++ 代码。一些编译器允许它作为扩展(可变长度数组)。
即使使用 VLA 扩展,代码也是无效的,因为 n
在您的代码中使用时未初始化。
首先是真正的解决方案:
使用 std::vector
(tadaaa):
#include <iostream>
#include <vector>
int main () {
int n;
std::vector<int> arr;
std::cout << "please put the size of the array " ;
std::cin >> n;
arr.reserve(n); // optional
std::cout << "please enter array's elemets ";
for (int k=0; k<n ; k++) {
int elem;
std::cin >> elem;
arr.push_back(elem);
}
for (auto e : arr) {
std::cout << e << std::endl;
}
}
如果您需要针对 C++98 进行编译(哇):
for (std::vector<int>::iterator it = arr.begin(); it != arr.end(); ++it) {
std::cout << *it << std::endl;
}
或者只是:
for (std::size_t i = 0; i < arr.size(); ++i) {
std::cout << arr[i] << std::endl;
}
如果您坚持使用 VLA(我建议不要使用):
int n;
cout << "please put the size of the array " ;
cin >> n;
int Arr[n];
cout << "please enter array's elemets ";
for (int k=0; k<n ; k++) {
cin >> Arr[k];
}
for (int i=0;i<n;i++){
cout << Arr[i] << endl;
}
正如许多其他人在评论部分提到的那样,另一种方法(如果您想坚持使用 C 数组)是在堆上动态分配数组。
#include <iostream>
using namespace std;
int main () {
int n;
cout << "please put the size of the array " ;
cin >> n;
int* Arr = new int[n]; //dynamically allocate an array to hold n int on the heap
cout << "please enter array's elemets ";
for (int k=0; k<n ; k++) {
cin >> Arr[k];
}
for (int i=0;i<n;i++){
cout << Arr[i] << endl;
}
delete [] Arr; //make sure to clean up the heap memories
}
来自dcl.init#12:
If no initializer is specified for an object, the object is
default-initialized. When storage for an object with automatic or
dynamic storage duration is obtained, the object has an indeterminate
value, and if no initialization is performed for the object, that
object retains an indeterminate value until that value is replaced
([expr.ass]).
unsigned char c;
unsigned char d = c; // OK, d has an indeterminate value
int e = d; // undefined behavior
因此,在您的代码中:
int n , Arr[n];
在 cin >> n;
中赋值之前,n
具有不确定的值
将 n
与此不确定值(不是 value-/zero-/default-initialized 且未分配)一起使用,可能会导致未定义的行为。
所以我一直在尝试获取数组大小及其元素的输入,然后将这些元素显示到屏幕上,但是当我举个例子 数组大小:7 数组的元素:1 2 3 4 5 6 7 输出是:
1
2
3
4
5
6
6
代码:
#include <iostream>
using namespace std;
int main () {
int n , Arr[n];
cout << "please put the size of the array " ;
cin >> n;
cout << "please enter array's elemets ";
for (int k=0; k<n ; k++) {
cin >> Arr[k];
}
for (int i=0;i<n;i++){
cout << Arr[i] << endl;
}
}
int Arr[n]
其中 n
不是编译时间常量是非法的 C++ 代码。一些编译器允许它作为扩展(可变长度数组)。
即使使用 VLA 扩展,代码也是无效的,因为 n
在您的代码中使用时未初始化。
首先是真正的解决方案:
使用 std::vector
(tadaaa):
#include <iostream>
#include <vector>
int main () {
int n;
std::vector<int> arr;
std::cout << "please put the size of the array " ;
std::cin >> n;
arr.reserve(n); // optional
std::cout << "please enter array's elemets ";
for (int k=0; k<n ; k++) {
int elem;
std::cin >> elem;
arr.push_back(elem);
}
for (auto e : arr) {
std::cout << e << std::endl;
}
}
如果您需要针对 C++98 进行编译(哇):
for (std::vector<int>::iterator it = arr.begin(); it != arr.end(); ++it) {
std::cout << *it << std::endl;
}
或者只是:
for (std::size_t i = 0; i < arr.size(); ++i) {
std::cout << arr[i] << std::endl;
}
如果您坚持使用 VLA(我建议不要使用):
int n;
cout << "please put the size of the array " ;
cin >> n;
int Arr[n];
cout << "please enter array's elemets ";
for (int k=0; k<n ; k++) {
cin >> Arr[k];
}
for (int i=0;i<n;i++){
cout << Arr[i] << endl;
}
正如许多其他人在评论部分提到的那样,另一种方法(如果您想坚持使用 C 数组)是在堆上动态分配数组。
#include <iostream>
using namespace std;
int main () {
int n;
cout << "please put the size of the array " ;
cin >> n;
int* Arr = new int[n]; //dynamically allocate an array to hold n int on the heap
cout << "please enter array's elemets ";
for (int k=0; k<n ; k++) {
cin >> Arr[k];
}
for (int i=0;i<n;i++){
cout << Arr[i] << endl;
}
delete [] Arr; //make sure to clean up the heap memories
}
来自dcl.init#12:
If no initializer is specified for an object, the object is default-initialized. When storage for an object with automatic or dynamic storage duration is obtained, the object has an indeterminate value, and if no initialization is performed for the object, that object retains an indeterminate value until that value is replaced ([expr.ass]).
unsigned char c;
unsigned char d = c; // OK, d has an indeterminate value
int e = d; // undefined behavior
因此,在您的代码中:
int n , Arr[n];
在 cin >> n;
中赋值之前,n
具有不确定的值
将 n
与此不确定值(不是 value-/zero-/default-initialized 且未分配)一起使用,可能会导致未定义的行为。