鱼C论坛

 找回密码
 立即注册
查看: 920|回复: 9

[已解决]一上午都没有找出来的小错误(悬赏20鱼币)

[复制链接]
发表于 2020-8-7 10:28:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 爱学习520 于 2020-8-7 10:35 编辑

做一个冒泡排序,比较结构体里面数据的大小,请问这个为什么错了,我测试过int、double、char这些都没有问题,这个比较结构体哪里错了,为什么不正确排序,如图:
正确答案应该是:
1 1
2 2
3 3


  1. #include<iostream>
  2. using namespace std;

  3. class Text
  4. {
  5.         public:
  6.                 int x;
  7.             int y;       
  8.         Text(){        }
  9.             
  10.         Text(int x,int y)
  11.         {
  12.                 this->x=x;
  13.                 this->y=y;
  14.         }
  15.         bool operator>(Text& text)
  16.         {
  17.                 return this->x > text.x&&this->y > text.y;
  18.         }
  19.         void print()
  20.         {
  21.                 cout<<x<<"  "<<y<<endl;
  22.         }
  23. };
  24. //冒泡排序
  25. template<class T>
  26. void sort(T arr,int length)
  27. {
  28.         int i,j;
  29.         Text* temp;
  30.        
  31.         for(i=0;i<length-1;i++)
  32.             for(j=i+1;j<length;j++)
  33.             if(arr[i]>arr[j])
  34.             {
  35.                     temp=arr[j];
  36.                     arr[j]=arr[i];
  37.                     arr[i]=temp;                   
  38.                 }                                   
  39. }
  40. main()
  41. {
  42.         int i;
  43.         Text a(2,2),b(3,3),c(1,1);        
  44.         Text* arr[3]={&a,&b,&c};       
  45.         sort(arr,3);
  46.        
  47.         arr[0]->print();
  48.         arr[1]->print();
  49.         arr[2]->print();


  50.    


  51. }
复制代码
最佳答案
2020-8-7 11:10:33
看错了,是你的重载 〉调用写错了。

if(arr[i]>arr[j])
改写成
if(*arr[i]>*arr[j])
2020-08-07_102659.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-8-7 10:47:39 | 显示全部楼层
为什么要用指针?指针交换乱了。

重载=操作符比较好

#include<iostream>
using namespace std;

class Text
{
        public:
                int x;
            int y;        
        Text(){        }

            
        Text(int x,int y)
        {
                        this->x=x;
                        this->y=y;
        }

                //重载 =
                Text & operator = (const Text & t)
                {
                        x = t.x;
                        y = t.y;
                        return * this;
                }

        bool operator>(Text& text)
        {
                        return this->x > text.x&&this->y > text.y;
        }
        void print()
        {
                        cout<<x<<"  "<<y<<endl;
        }
};
//冒泡排序
template<class T>
void sort(T arr,int length)
{
        int i,j;
        //
        Text temp;
       
        for(i=0;i<length-1;i++)
                for(j=i+1;j<length;j++)
            if(arr[i]>arr[j])
            {
                                temp=arr[j];
                                arr[j]=arr[i];
                                arr[i]=temp;                    
                        }                                    
}
void main()
{
        int i=0;
        Text a(2,2),b(3,3),c(1,1);         
        Text arr[3]={a,b,c};        
        sort(arr,3);
       
        arr[0].print();
        arr[1].print();
        arr[2].print();
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-7 10:58:20 | 显示全部楼层
xieglt 发表于 2020-8-7 10:47
为什么要用指针?指针交换乱了。

重载=操作符比较好

但是我哪个哪里错了呢,求指导
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-7 11:01:18 | 显示全部楼层
错在比较大小后的交换
交换的是指针,不是X,Y的值
3个指针变来变去,乱套了,自己跟踪调试一下。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2020-8-7 11:10:33 | 显示全部楼层    本楼为最佳答案   
看错了,是你的重载 〉调用写错了。

if(arr[i]>arr[j])
改写成
if(*arr[i]>*arr[j])
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2020-8-7 11:15:08 | 显示全部楼层
两个指针比较是他们的地址在比,而没有调用重载>函数。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-7 11:15:51 | 显示全部楼层
所以建议不要随便用指针,很不优雅。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-7 11:35:03 | 显示全部楼层
五楼说得对!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-7 11:45:22 | 显示全部楼层
xieglt 发表于 2020-8-7 11:10
看错了,是你的重载 〉调用写错了。

if(arr>arr[j])

非常感谢,20鱼币已经通过给你四个帖子分别支持了5鱼币,表示感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-8 09:06:12 | 显示全部楼层
学习
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-8 21:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表