|
发表于 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();
} |
|