C++ C++;对指向对象的指针数组进行排序

C++ C++;对指向对象的指针数组进行排序,c++,class,sorting,shapes,C++,Class,Sorting,Shapes,可能重复: 我接到家长的电话 Shape Shape接到了两个孩子的电话 Square and Rectangle Shape类得到了一个变量调用区域,它是int类型的 所以我创建了一些正方形,长方形的物体,像这样 int main() { Shape *shaped[100]; //then i did some adding of object.. int areaValue; areaValue=1; shaped[0] = new Rec

可能重复:

我接到家长的电话

Shape
Shape接到了两个孩子的电话

Square and Rectangle
Shape类得到了一个变量调用区域,它是int类型的

所以我创建了一些正方形,长方形的物体,像这样

int main()
{
    Shape *shaped[100];

    //then i did some adding of object..
    int areaValue;
    areaValue=1;

    shaped[0] = new Rectangle();
    shaped[0]->setArea(areaValue);

    areaValue=7;
    shaped[1] = new Square();
    shaped[1]->setArea(areaValue);

    areaValue=5;
    shaped[2] = new Square();
    shaped[2]->setArea(areaValue);

    shapeCounter = 3;


     sort(shaped, shaped + 3, sort_by_area());


    for (int i=0;i<shapeCounter;i++)
    {
        cout << shaped[i].getArea() << endl;
    }

}
intmain()
{
形状*形状[100];
//然后我添加了一些对象。。
内在价值;
面积值=1;
形状[0]=新矩形();
成形[0]->设定面积(面积值);
面积值=7;
形状[1]=新正方形();
成形[1]->设定面积(面积值);
面积值=5;
形状[2]=新正方形();
成形[2]->设定面积(面积值);
形状计数器=3;
排序(成形,成形+3,按面积()排序);
对于(int i=0;i而言,这非常有效:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

class Shape{

private:
    int x;
public:
    void setArea(int x){
        this->x =  x;
    }
    int getArea(){
        return this->x;
    }
};

class Rectangle: public Shape{
public:
};

class Square: public Shape{
public:
};

bool sort_by_area (Shape* x,Shape* y) { return (x->getArea() < y->getArea()); }
int main()
{
    Shape *shaped[100];

    //then i did some adding of object..
    int areaValue,shapeCounter = 0;
    areaValue=1;

    shaped[0] = new Rectangle();
    shaped[0]->setArea(areaValue);

    areaValue=7;
    shaped[1] = new Square();
    shaped[1]->setArea(areaValue);

    areaValue=5;
    shaped[2] = new Square();
    shaped[2]->setArea(areaValue);

    shapeCounter = 3;


    sort(shaped, shaped + 3, sort_by_area);


    for (int i=0;i<shapeCounter;i++)
    {
        cout << shaped[i]->getArea() << endl;
    }
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
阶级形态{
私人:
int x;
公众:
无效设置区域(整数x){
这个->x=x;
}
int getArea(){
返回此->x;
}
};
类矩形:公共形状{
公众:
};
阶级广场:公共形态{
公众:
};
bool按区域排序(Shape*x,Shape*y){return(x->getArea()getArea());}
int main()
{
形状*形状[100];
//然后我添加了一些对象。。
int areaValue,shapeCounter=0;
面积值=1;
形状[0]=新矩形();
成形[0]->设定面积(面积值);
面积值=7;
形状[1]=新正方形();
成形[1]->设定面积(面积值);
面积值=5;
形状[2]=新正方形();
成形[2]->设定面积(面积值);
形状计数器=3;
排序(成形,成形+3,按面积排序);

对于(int i=0;i)您所说的“如果我不使用struct,我仍然可以排序”是什么意思?你指的是形状结构吗?@Michael-我想OP指的是
sort\u by\u area
对象。user1595932-使用比较对象有什么错吗?
sort
函数需要一个函数对象,而这是一个提供函数对象的简单方法。@Ted Hopp:是的,我没有注意到他将comperator定义为一个结构,它应该是一个常规comperator函数。@user1595932:下面是一个使用排序的正确方法的示例,@Michael,但是我如何重做我的代码以便使用正常的排序方法,但是我现在正在对指向对象的指针数组进行排序。没有将其称为struct sort_by_area,或者其他形式,或者实际上可以使用它吗struct@user1595932:如果这个答案满足了你的需要,接受它
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

class Shape{

private:
    int x;
public:
    void setArea(int x){
        this->x =  x;
    }
    int getArea(){
        return this->x;
    }
};

class Rectangle: public Shape{
public:
};

class Square: public Shape{
public:
};

bool sort_by_area (Shape* x,Shape* y) { return (x->getArea() < y->getArea()); }
int main()
{
    Shape *shaped[100];

    //then i did some adding of object..
    int areaValue,shapeCounter = 0;
    areaValue=1;

    shaped[0] = new Rectangle();
    shaped[0]->setArea(areaValue);

    areaValue=7;
    shaped[1] = new Square();
    shaped[1]->setArea(areaValue);

    areaValue=5;
    shaped[2] = new Square();
    shaped[2]->setArea(areaValue);

    shapeCounter = 3;


    sort(shaped, shaped + 3, sort_by_area);


    for (int i=0;i<shapeCounter;i++)
    {
        cout << shaped[i]->getArea() << endl;
    }
    return 0;
}