【问题标题】:C++ bubble sort function with pointers带指针的 C++ 冒泡排序函数
【发布时间】:2018-05-25 10:07:00
【问题描述】:

我想编写一个包含冒泡排序的程序,并在函数内使用指针。 这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

void rendez(int* t, int n){
    for(int i=0; i<n-1; i++){
        for(int j=i+1; j<n; j++){
            if(*t+i<*t+j){
                int temp = *t+i;
                *t+i = *t+j;
                *t+j = temp;
            }
        }
    }
}

int main(){

    setlocale(LC_ALL,"");

    int t[10] = {2,3,4,5,6,7,8,9,10,11};

    rendez(&t,sizeof(t)); 

    printf("\n");
    system("pause");
}

它给了我这些错误:

C:\Users\metal\gyakorlás1211.cpp    In function 'void rendez(int*, int)':
C:\Users\metal\gyakorlás1211.cpp    [Error] lvalue required as left operand of assignment
C:\Users\metal\gyakorlás1211.cpp    [Error] lvalue required as left operand of assignment
C:\Users\metal\gyakorlás1211.cpp    In function 'int main()':
C:\Users\metal\gyakorlás1211.cpp    [Error] cannot convert 'int (*)[10]' to 'int*' for argument '1' to 'void rendez(int*, int)'

谢谢!

【问题讨论】:

  • 你可能是指*(t + i)等。或者只是t[i]
  • t[i] 有什么问题?
  • &lt;iostream&gt;。这是 C++,不是 C。
  • std::sort 有什么问题?
  • 我认为问题是针对C 被编辑为C++ 的语言

标签: c++ function pointers bubble-sort


【解决方案1】:

你需要的两个改变

      if(*(t+i)<*(t+j)){
            int temp = *(t+i);
            *(t+i) = *(t+j);
            *(t+j) = temp;
        }

还有

rendez(t,sizeof(t)/sizeof(t[0])); 

现在看看你之前做了什么,首先你的编译器一定已经给你很多警告了。

&amp;tint (*)[10],这不是你想要的。

您只是想传递最终会衰减为指针的数组,然后您所做的任何更改都会反映到数组中。

之前你的*t+i 正在做这样的事情,(*t)+i 这就是你想要的吗?此外,tint(*)[10],所以您基本上是在向其添加 ij。这是不对的。您正在使用地址,但您想使用值。

函数的第二个参数,你想传递数组的大小,但不是字节数,而是元素数。

sizeof (arr) 基本上是说10*sizeof(int),因为它包含int。但这就是你想要的吗?不,您想传递int 元素的数量。所以只需将其除以每个int 的大小。这就是我们在sizeof(t)/sizeof(t[0]) 中所做的。

【讨论】:

    【解决方案2】:

    当您在参数中发送&amp;t 时,您实际上发送的不是数组的第一个地址,而是整个数组的地址。

    让我们了解一下这个东西
    假设数组被分配为 1000, 1004, 1008. . . 所以, 第一个元素的地址将是 1000,因为当您在其中添加 1 时,它将为您提供 1004。 并且整个数组的地址将是1000,因为当您在其中添加1时,它将给出数组中最后一个元素的下一个地址。

    t 是指向第一个元素的指针,&amp;t 是指向数组的指针。 因此,要将数组传递给其他函数,只需在参数中发送 t

    现在,sizeof 运算符不返回数组的长度,而是它分配的大小。

    阅读this并将你的函数调用写成。

    rendez(t, sizeof(t)/sizeof(t[0]));


    关于lvalue 错误。

    * 的优先级高于+,所以* 首先执行。 所以像这样写*t + i 会给你从数组的第一个元素开始的第 i 个下一个地址。
    所以你需要像这样写 *(t + i) 在赋值操作中。

    【讨论】:

      【解决方案3】:

      这个电话

      rendez(&t,sizeof(t)); 
      

      是错误的,因为表达式&amp;t 的类型为int (*)[10] 而不是int * 类型,即函数rendez 的第一个参数的类型。

      你应该像这样调用函数

      rendez( t, sizeof(t) / sizeof( *t ) ); 
      

      并且函数的第二个参数的类型应该是size_t

      也在函数this if语句中

              if(*t+i<*t+j){
                  int temp = *t+i;
                  *t+i = *t+j;
                  *t+j = temp;
              }
      

      也是错的。

      它必须看起来像

              if( *( t + i ) < *( t + j ) ){
                  int temp = *( t + i );
                  *( t + i ) = *( t + j );
                  *( t + j ) = temp;
              }
      

      考虑到冒泡排序算法会比较相邻元素。

      您的函数实现看起来像一个低效的选择排序。

      如果你只想使用指针,那么函数可以看起来像演示程序中显示的那样

      #include <iostream>
      
      void rendez( int *a, int n )
      {
          for ( int *first = a, *swapped = a, *last = a + n; !( last - first < 2 ); first = a, last = swapped )
          {
              swapped = a;
              while ( ++first != last )
              {
                  if ( *( first - 1 ) < *first )
                  {
                      swapped = first;
                      int tmp = *first;
                      *first = *( first - 1 );
                      *( first - 1 ) = tmp;
                  }
              }
          }        
      }
      
      
      int main() 
      {
          int a[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
      
          for ( int x : a ) std::cout << x << ' ';
          std::cout << std::endl;
      
          rendez( a, sizeof( a ) / sizeof( *a ) );
      
          for ( int x : a ) std::cout << x << ' ';
          std::cout << std::endl;
      
          return 0;
      }
      

      它的输出是

      2 3 4 5 6 7 8 9 10 11 
      11 10 9 8 7 6 5 4 3 2 
      

      该函数确实实现了冒泡排序算法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-13
        相关资源
        最近更新 更多