简体   繁体   English

反向C ++数组

[英]reverse c++ array

my aim is to reverse an array 3,12,2,1 to 1,2,12,3. 我的目标是将数组3,12,2,1反转为1,2,12,3。 when i run this code i get garbage before my actually result. 当我运行此代码时,我得到的垃圾在实际结果之前。 i can't seem to see where the problem is please assit 我似乎看不出问题出在哪里

#include<iostream>
using namespace std;


int rev (int arr[], int a){
    //int r;
    for(int i =a-1; i>=0; i--){
    cout<<arr[i]<<" "; 
    }   
   return 0;
 }

 int main(){
 int arr[] = {6,41,12,5,2};

cout<<"The rev of {6,41,12,5,2}"<<endl;
 cout<<rev(arr, sizeof(arr))<<endl;


   system("pause");
   return 0;
}

An optimized answer to the question would be using reverse () from STL if you are allowed to use it: 如果允许您使用STL的反向(),则该问题的最佳答案是:

std::reverse 

http://www.sgi.com/tech/stl/reverse.html http://www.sgi.com/tech/stl/reverse.html

int main()
{
    int arr[] = {6,41,12,5,2};
    cout<<"The rev of {6,41,12,5,2}"<<endl;
    reverse(arr, arr + 5);
    copy(arr, arr + 5, ostream_iterator<int>(cout, ", "));
}

Use sizeof(arr)/sizeof(arr[0]) instead of sizeof(arr) . 使用sizeof(arr)/sizeof(arr[0])代替sizeof(arr)

sizeof(arr) gives the total size of the array. sizeof(arr)给出数组的总大小。 sizeof(arr[0]) is the size of one array element (all elements have the same size). sizeof(arr[0])是一个数组元素的大小(所有元素都具有相同的大小)。 So sizeof(arr)/sizeof(arr[0]) is the number of elements. 因此sizeof(arr)/sizeof(arr[0])是元素的数量。

sizeof return the size in bytes. sizeof返回以字节为单位的大小。 In your example, if sizeof(int) = 4, it returns 20. 在您的示例中,如果sizeof(int)= 4,则返回20。

Because you're using an array, you have to keep the size of the array handy as well. 因为使用的是数组,所以还必须保​​持数组大小方便。 sizeof computes the size of a value in memory, in this case the size of all the memory used to represent arr . sizeof计算内存中值的大小,在这种情况下,用于表示arr的所有内存的大小。 You can do sizeof(arr)/sizeof(int) to get the number of elements in an array. 可以执行sizeof(arr)/sizeof(int)来获取数组中元素的数量。 This makes sense because it's taking the total size of the array and dividing it by the size of an element in the array. 这是有道理的,因为它将占用数组的总大小,然后将其除以数组中元素的大小。 Beware however that this only works for arrays ( int arr[4] = {6,41,12,5,2}; ). 但是请注意,这仅适用于数组( int arr[4] = {6,41,12,5,2}; )。 If it's a pointer to a heap-allocated array via something like int* i = new int[4]; 如果它是通过类似int* i = new int[4];的指针指向堆分配的数组,则int* i = new int[4]; you'll need to keep the size of the array hanging around. 您需要保持数组的大小不变。

Also, you're calling your reverse function from within a cout<< call, which will print the function's return value (in this case it's hard-coded to 0). 同样,您正在cout<<调用中调用反向函数,这将打印函数的返回值(在这种情况下,它的硬编码为0)。

It also turns out there is a function in the C++ standard library ( std::reverse ) that can do this. 事实证明,C ++标准库中有一个函数( std :: reverse )可以做到这一点。

If I may speak subjectively and in an off-topic manner about your approach, it is very un-C-like. 如果我可以主观地和不合时宜地谈论您的方法,那是非常不像C的。 My personal favorite way to reverse an array goes like this: 我个人最喜欢的反转数组的方式如下:

void reverse(int *a, int n)
{
   int *p = a, *q = a + n - 1;

   while (p < q)
   {
      int swap = *p;
      *p++ = *q;
      *q-- = swap;
   }
}

// Usage:

   int a [] = { /* ... */ };

   reverse(a, sizeof(a)/sizeof(*a));

Of course, since your question is tagged c++ , there's always std::reverse() . 当然,由于您的问题被标记为c++ ,因此始终存在std::reverse()

sizeof运算符返回一个额外的值(arrayLength + 1),这里6将在传递6时返回,存储在a-1中时,您将得到5,但数组索引从0长度1开始,即从0到4,这里我指向索引5不是最后一个元素last + 1为何获得垃圾值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM