C++ 我想反转我的数组。为什么这段代码会给出垃圾值?
输入:-4 1 2 3 4C++ 我想反转我的数组。为什么这段代码会给出垃圾值?,c++,arrays,algorithm,vector,reverse,C++,Arrays,Algorithm,Vector,Reverse,输入:-4 1 2 3 4 输出4199008 4 3 2 1a[n]将返回最后一个元素之后的元素。当以相反的顺序迭代时,从i=n-1开始。a[n]将返回最后一个元素之后的元素。当以相反的顺序迭代时,从i=n-1开始。对于初学者,程序具有未定义的行为,因为变量n未初始化 #include <iostream> using namespace std; int main(){ int n; int a[n]; cin>>
输出4199008 4 3 2 1a[n]将返回最后一个元素之后的元素。当以相反的顺序迭代时,从i=n-1开始。a[n]将返回最后一个元素之后的元素。当以相反的顺序迭代时,从i=n-1开始。对于初学者,程序具有未定义的行为,因为变量n未初始化
#include <iostream>
using namespace std;
int main(){
int n;
int a[n];
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=n;i>=0;i--){
cout<<a[i]<<" ";
}
}
所以这个宣言
int n;
这是无效的。此外,可变长度数组不是标准C++特性。而是使用标准类模板std::vector
也在这个循环中
int a[n];
程序输出可能如下所示
#include <iostream>
#include <vector>
int main()
{
size_t n = 0;
std::cout << "Enter the size of an array ";
std::cin >> n;
std::vector<int> v( n );
std::cout << "Enter " << n << " elements: ";
for ( auto &item : v ) std::cin >> item;
std::cout << "The array in the reverse order\n";
for ( size_t i = v.size(); i != 0; )
{
std::cout << v[--i] << ' ';
}
std::cout << '\n';
return 0;
}
如果您想使用标准算法,那么您的程序可以按照以下方式运行
Enter the size of an array 10
Enter 10 elements: 0 1 2 3 4 5 6 7 8 9
The array in the reverse order
9 8 7 6 5 4 3 2 1 0
对于初学者,程序具有未定义的行为,因为变量n未初始化
#include <iostream>
using namespace std;
int main(){
int n;
int a[n];
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=n;i>=0;i--){
cout<<a[i]<<" ";
}
}
所以这个宣言
int n;
这是无效的。此外,可变长度数组不是标准C++特性。而是使用标准类模板std::vector
也在这个循环中
int a[n];
程序输出可能如下所示
#include <iostream>
#include <vector>
int main()
{
size_t n = 0;
std::cout << "Enter the size of an array ";
std::cin >> n;
std::vector<int> v( n );
std::cout << "Enter " << n << " elements: ";
for ( auto &item : v ) std::cin >> item;
std::cout << "The array in the reverse order\n";
for ( size_t i = v.size(); i != 0; )
{
std::cout << v[--i] << ' ';
}
std::cout << '\n';
return 0;
}
如果您想使用标准算法,那么您的程序可以按照以下方式运行
Enter the size of an array 10
Enter 10 elements: 0 1 2 3 4 5 6 7 8 9
The array in the reverse order
9 8 7 6 5 4 3 2 1 0
在程序开始时,出现了一个错误:
Enter the size of an array 10
Enter 10 elements: 0 1 2 3 4 5 6 7 8 9
The array in the reverse order
9 8 7 6 5 4 3 2 1 0
现在我可以假设这只是复制它时的一个错误,因为您提供了输入和输出
输入:-4 1 2 3 4输出4199008 4 3 2 1
忘了这一点,您声明了一个大小为n的数组。请记住,数组的元素将从0变为n-1。现在看看你的第二个for循环
int n; // You declare n with no value
int a[n]; // You use is
cin>>n; // After you used it you get your value-
因此,您仍然会得到n个值作为输出,但您访问的第一个元素位于数组之外。这就是为什么会得到一个垃圾值,这是一个留在那里的值
解决方案是更改for循环
// the first element you acces is n and you stop at 1
// but the array goes from n-1 to 0
for(int i=n;i>=0;i--){
cout<<a[i]<<" ";
}
在程序开始时,出现了一个错误:
Enter the size of an array 10
Enter 10 elements: 0 1 2 3 4 5 6 7 8 9
The array in the reverse order
9 8 7 6 5 4 3 2 1 0
现在我可以假设这只是复制它时的一个错误,因为您提供了输入和输出
输入:-4 1 2 3 4输出4199008 4 3 2 1
忘了这一点,您声明了一个大小为n的数组。请记住,数组的元素将从0变为n-1。现在看看你的第二个for循环
int n; // You declare n with no value
int a[n]; // You use is
cin>>n; // After you used it you get your value-
因此,您仍然会得到n个值作为输出,但您访问的第一个元素位于数组之外。这就是为什么会得到一个垃圾值,这是一个留在那里的值
解决方案是更改for循环
// the first element you acces is n and you stop at 1
// but the array goes from n-1 to 0
for(int i=n;i>=0;i--){
cout<<a[i]<<" ";
}
当反转数组时,从n-1开始循环,即i=n-1 n是数组中的元素数。然后运行循环,直到i>=0。若从n开始循环,它将读取超出范围的非法索引,并将给您垃圾值
for(int i=n-1;i>=-1;i--){
cout<<a[i]<<" ";
}
当反转数组时,从n-1开始循环,即i=n-1 n是数组中的元素数。然后运行循环,直到i>=0。若从n开始循环,它将读取超出范围的非法索引,并将给您垃圾值
for(int i=n-1;i>=-1;i--){
cout<<a[i]<<" ";
}
在编译器上启用警告。int a[n];不是每个编译器都支持,如果不是错误,应该生成警告。您不需要先初始化n。实际上,您需要std::vector。而且您可以使用反转任何支持双向迭代的容器。此外,您的第二个循环索引a[n],即UB。您需要从n-1开始。您的代码显然不会尝试反转数组。请参阅std::reverse。应用它之后,从第一个到最后一个输出结果数组。这将简化并记录您的编码意图,并避免意外的未定义行为。在编译器上启用警告。int a[n];不是每个编译器都支持,如果不是错误,应该生成警告。您不需要先初始化n。实际上,您需要std::vector。而且您可以使用反转任何支持双向迭代的容器。此外,您的第二个循环索引a[n],即UB。您需要从n-1开始。您的代码显然不会尝试反转数组。请参阅std::reverse。应用它之后,从第一个到最后一个输出结果数组。这将简化并记录您的编码意图,并避免意外的未定义行为。还有更多需要修复的问题,例如,在初始化n之前的int a[n]。即使是初始化它也不起作用,除非n是常量。还有更多的问题需要解决,例如,在初始化n之前的int a[n]。即使初始化它也不起作用,除非n是常量。荣誉提示:rbegin和rend使反向复制完全不必要。荣誉提示:rbegin和rend使反向复制完全不必要。