C++ 反转数组中的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19712903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reverse Contents in Array
提问by John
I have an array of numbers that I am trying to reverse. I believe the function in my code is correct, but I cannot get the proper output.
我有一组我想反转的数字。我相信我的代码中的函数是正确的,但我无法获得正确的输出。
The output reads: 10 9 8 7 6. Why can't I get the other half of the numbers? When I remove the "/2" from count, the output reads: 10 9 8 7 6 6 7 8 9 10
输出为:10 9 8 7 6. 为什么我不能得到另一半的数字?当我从计数中删除“/2”时,输出显示为:10 9 8 7 6 6 7 8 9 10
void reverse(int [], int);
int main ()
{
const int SIZE = 10;
int arr [SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
reverse(arr, SIZE);
return 0;
}
void reverse(int arr[], int count)
{
int temp;
for (int i = 0; i < count/2; ++i)
{
arr[i] = temp;
temp = arr[count-i-1];
arr[count-i-1] = arr[i];
arr[i] = temp;
cout << temp << " ";
}
}
回答by juanchopanza
This would be my approach:
这将是我的方法:
#include <algorithm>
#include <iterator>
int main()
{
const int SIZE = 10;
int arr [SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::reverse(std::begin(arr), std::end(arr));
...
}
回答by simonc
The line
线
arr[i] = temp;
is wrong. (On the first iteration of your loop it sets arr[i]
to an undefined value; further iterations set it to an incorrect value.) If you remove this line, your array should be reversed correctly.
是错的。(在循环的第一次迭代中,它设置arr[i]
为未定义的值;进一步的迭代将其设置为不正确的值。)如果删除此行,则数组应该正确反转。
After that, you should move the code which prints the reversed array into a new loop which iterates over the whole list. Your current code only prints the first count/2
elements.
之后,您应该将打印反向数组的代码移动到一个新循环中,该循环遍历整个列表。您当前的代码仅打印第一个count/2
元素。
int temp, i;
for (i = 0; i < count/2; ++i) {
temp = arr[count-i-1];
arr[count-i-1] = arr[i];
arr[i] = temp;
}
for (i = 0; i < count; ++i) {
cout << arr[i] << " ";
}
回答by Matt
Both answers look correct to me.
这两个答案在我看来都是正确的。
The first
arr[i] = temp;
should be removedYou should do a second loop to print allelements, not just half the array. The loop that does the reverse doesn't need to print it.
第一个
arr[i] = temp;
应该删除您应该执行第二个循环来打印所有元素,而不仅仅是数组的一半。执行相反操作的循环不需要打印它。
回答by Floris
You are not printing the array, you are printing the value of temp
- which is only half the array...
您不是在打印数组,而是在打印的值temp
- 这只是数组的一半......
回答by user93353
void reverse(int [], int);
void printarray(int [], int );
int main ()
{
const int SIZE = 10;
int arr [SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
cout<<"Before reverse\n";
printarray(arr, SIZE);
reverse(arr, SIZE);
cout<<"After reverse\n";
printarray(arr, SIZE);
return 0;
}
void printarray(int arr[], int count)
{
for(int i = 0; i < count; ++i)
cout<<arr[i]<<' ';
cout<<'\n';
}
void reverse(int arr[], int count)
{
int temp;
for (int i = 0; i < count/2; ++i)
{
temp = arr[i];
arr[i] = arr[count-i-1];
arr[count-i-1] = temp;
}
}
回答by user93353
The solution to this question is very easy: Vectors
这个问题的答案很简单:向量
std::vector<int> vector;
for(int i = 0; i < 10;i++)
{
vector.push_back(i);
}
std::reverse(vector.begin(), vector.end());
Voila! You are done! =)
瞧!你完成了!=)
Solution details:
解决方案详情:
This is the most efficent solution: Swap can't swap 3 values but reverse definitely can. Remember to include algorithm. This is so simple that the compiled code is definitely not needed.
这是最有效的解决方案:Swap 不能交换 3 个值,但 reverse 绝对可以。请记住包含算法。这太简单了,编译后的代码肯定是不需要的。
I think this solves the OP's problem
我认为这解决了 OP 的问题
If you think there are any errors and problems with this solution please comment below
如果您认为此解决方案有任何错误和问题,请在下方评论
回答by Argento
As a direct answer to your question: Your swapping is wrong
作为对您问题的直接回答:您的交换是错误的
void reverse(int arr[], int count){
int temp;
for(int i = 0; i < count/2; ++i){
arr[i] = temp; // <== Wrong, Should be deleted
temp = arr[count-i-1];
arr[count-i-1] = arr[i];
arr[i] = temp;
}
}
assigning arr[i] = temp
causes error when it first enters the loop as temp initially contains garbage data and will ruin your array, remove it and the code should work well.
arr[i] = temp
当它第一次进入循环时,分配会导致错误,因为 temp 最初包含垃圾数据并且会破坏你的数组,删除它并且代码应该可以正常工作。
As an advice, use built-in functions whenever possible:
作为建议,请尽可能使用内置函数:
- In the swapping you could just use swaplike
std::swap(arr[i], arr[count-i-1])
- For the reverse as a whole
just use reverselike
std::reverse(arr, arr+count)
I am using C++14 and reverse works with arrays without any problems.
我正在使用 C++14 并且反向使用数组没有任何问题。
回答by Abraham Hernandez
I would use the reverse()
function from the <algorithm>
library.
我会使用库中的reverse()
函数<algorithm>
。
Run it online: repl.it/@abranhe/Reverse-Array
在线运行:repl.it/@abranhe/Reverse-Array
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int arr [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
reverse(begin(arr), end(arr));
for(auto item:arr)
{
cout << item << " ";
}
}
Output:
输出:
10 9 8 7 6 5 4 3 2 1
Hope you like this approach.
希望你喜欢这种方法。
回答by thebarylowi
First of all what value do you have in this pice of code? int temp;
? You can't tell because in every single compilation it will have different value - you should initialize your value to not have trash value from memory. Next question is: why you assign this temp value to your array?
If you want to stick with your solution I would change reverse function like this:
首先,你在这段代码中有什么价值?int temp;
? 您无法判断,因为在每次编译中它都会具有不同的值 - 您应该初始化您的值,以免内存中出现垃圾值。下一个问题是:为什么要将此临时值分配给数组?如果你想坚持你的解决方案,我会像这样改变反向功能:
void reverse(int arr[], int count)
{
int temp = 0;
for (int i = 0; i < count/2; ++i)
{
temp = arr[count - i - 1];
arr[count - i - 1] = arr[i];
arr[i] = temp;
}
for (int i = 0; i < count; ++i)
{
std::cout << arr[i] << " ";
}
}
Now it will works but you have other options to handle this problem.
现在它可以工作了,但您还有其他选择来处理这个问题。
Solution using pointers:
使用指针的解决方案:
void reverse(int arr[], int count)
{
int* head = arr;
int* tail = arr + count - 1;
for (int i = 0; i < count/2; ++i)
{
if (head < tail)
{
int tmp = *tail;
*tail = *head;
*head = tmp;
head++; tail--;
}
}
for (int i = 0; i < count; ++i)
{
std::cout << arr[i] << " ";
}
}
And ofc like Carlos Abraham says use build in function in algorithm
library
像卡洛斯亚伯拉罕这样的ofc说在algorithm
库中使用内置函数
回答by rashedcs
Procedure :
1.Take an array.
2.Then by default function reverse(array_name, array_name + size) .
reverse(array_name, array_name + size) function exits in algorithm.h header file.
3.Now print the array.
N.B Here we use new and delete for dynamic memory allocation.
C++ implementation :
C++ 实现:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
int *arr = new int[n];
for(int i=0; i<n; i++) cin>>arr[i];
reverse(arr, arr+n);
for(int i=0; i<n; i++) cout<<arr[i]<<" ";
delete[] arr;
return 0;
}