我想扭转我的阵列。为什么这段代码给出了垃圾值?
I wanted to reverse my array. Why this code gives garbage value?
#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]<<" ";
}
}
输入:- 4
1 2 3 4
输出 4199008 4 3 2 1
a[n]
将 return 最后一个元素之后的元素。当您以相反的顺序迭代时,从 i=n-1
.
开始
对于初学者来说,该程序具有未定义的行为,因为变量 n
未初始化
int n;
所以这个声明
int a[n];
无效。此外,可变长度数组不是标准的 C++ 功能。而是使用标准 class 模板 std::vector
.
也在这个循环中
for(int i=n;i>=0;i--) {
cout<<a[i]<<" ";
}
您正在尝试访问索引为 n
的 non-existent 元素。
另外你没有反转数组。您正在尝试以相反的顺序输出数组。
注意在header <algorithm>
.
中声明了标准算法std::reverse
和std::reverse_copy
这是一个示例,您的程序使用您的方法看起来如何
#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
如果使用标准算法,那么您的程序可以如下所示
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
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: ";
std::copy_n( std::istream_iterator<int>( std::cin ), n, std::begin( v ) );
std::cout << "The array in the reverse order\n";
std::reverse_copy( std::begin( v ), std::end( v ),
std::ostream_iterator<int>( std::cout, " ") );
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
你的程序开头有一个错误:
int n; // You declare n with no value
int a[n]; // You use is
cin>>n; // After you used it you get your value-
现在我可以假设那只是复制时的一个错误,因为你提供了输入和输出
Input:- 4 1 2 3 4 Output 4199008 4 3 2 1
因此忘记了这一点,您声明了一个大小为 n 的数组。 记住数组的元素将从 0 到 n-1。现在看看你的第二个 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 个值作为输出,但您访问的第一个元素在数组之外。这就是为什么你得到一个垃圾值,这是一个留在那里的值。
一个解决方案是更改 for 循环
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>=0; i++){
cout<<arr[i]<<" ";}
#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]<<" ";
}
}
输入:- 4 1 2 3 4 输出 4199008 4 3 2 1
a[n]
将 return 最后一个元素之后的元素。当您以相反的顺序迭代时,从 i=n-1
.
对于初学者来说,该程序具有未定义的行为,因为变量 n
未初始化
int n;
所以这个声明
int a[n];
无效。此外,可变长度数组不是标准的 C++ 功能。而是使用标准 class 模板 std::vector
.
也在这个循环中
for(int i=n;i>=0;i--) {
cout<<a[i]<<" ";
}
您正在尝试访问索引为 n
的 non-existent 元素。
另外你没有反转数组。您正在尝试以相反的顺序输出数组。
注意在header <algorithm>
.
std::reverse
和std::reverse_copy
这是一个示例,您的程序使用您的方法看起来如何
#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
如果使用标准算法,那么您的程序可以如下所示
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
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: ";
std::copy_n( std::istream_iterator<int>( std::cin ), n, std::begin( v ) );
std::cout << "The array in the reverse order\n";
std::reverse_copy( std::begin( v ), std::end( v ),
std::ostream_iterator<int>( std::cout, " ") );
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
你的程序开头有一个错误:
int n; // You declare n with no value
int a[n]; // You use is
cin>>n; // After you used it you get your value-
现在我可以假设那只是复制时的一个错误,因为你提供了输入和输出
Input:- 4 1 2 3 4 Output 4199008 4 3 2 1
因此忘记了这一点,您声明了一个大小为 n 的数组。 记住数组的元素将从 0 到 n-1。现在看看你的第二个 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 个值作为输出,但您访问的第一个元素在数组之外。这就是为什么你得到一个垃圾值,这是一个留在那里的值。
一个解决方案是更改 for 循环
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>=0; i++){
cout<<arr[i]<<" ";}