如何使用std :: sort对数组中的特定对象进行排序?

时间:2017-05-20 09:53:32

标签: c++ stl

我有这些类,我想对一个对象数组进行排序,考虑x坐标,并对那些具有特定值的属性进行排序。

Class.h
#include <iostream>
#include <algorithm>

class Punct2D
{
protected:
    int x, y;
public:
    Punct2D() {};
    ~Punct2D() {};

    int get_x() const;
    int get_y() const;
    void set_x(const int x);
    void set_y(const int y);
    friend std::ostream &operator << (std::ostream &flux, Punct2D dot);
    friend std::istream &operator >> (std::istream &flux, Punct2D &dot);
};

class Punct2DColorat :public Punct2D
{
private:
    char *color;
public:
    Punct2DColorat() { this->color = NULL; };
    ~Punct2DColorat() {};

    char *get_color();
    void set_color(char *color);
    bool operator<(Punct2DColorat dot);

}; 

我在这里实施。

#include "Class.h"

int Punct2D::get_x() const
{
    return this->x;
}
int Punct2D::get_y() const
{
    return this->y;
}
void Punct2D::set_x(const int x)
{
    this->x = x;
}
void Punct2D::set_y(const int y)
{
    this->y = y;
}

char *Punct2DColorat::get_color()
{
    return this->color;
}
void Punct2DColorat::set_color(char *color)
{
    this->color = new char[strlen(color) + 1];
    for (int i = 0; i < strlen(color) + 1; i++) this->color[i] = color[i];
}
bool Punct2DColorat::operator<(Punct2DColorat dot)
{
    return this->x < dot.get_x();
}

std::ostream &operator << (std::ostream &flux, Punct2D dot)
{
    flux << "Punct(" << dot.get_x() << "," << dot.get_y() << ")\n";
    return flux;
}
std::istream &operator >> (std::istream &flux, Punct2D &dot)
{
    std::cout << "Introduceti x :";
    flux >> dot.x;
    std::cout << "Introduceti y :";
    flux >> dot.y;
    return flux;
}

这是主要的。

#include "Class.h"

void main()
{
    int n, it = 0; char *aux = new char[15]; bool value;
    Punct2DColorat *dots;

    std::cout << "Cate puncte introduceti :"; std::cin >> n;
    dots = new Punct2DColorat[n];

    for (int i = 0; i < n; i++)
    {
        std::cout << "Introduceti 0 pentru Punct2D, respectiv 1 pentru Punct2D colorat :";
        std::cin >> value;
        if (value)
        {
            std::cin >> dots[i];
            std::cout << "Introduceti culoarea punctului :";
            std::cin >> aux;
            dots[i].set_color(aux);
        }
        else
        {
            std::cin >> dots[i];
        }
    }

    std::sort(dots, dots + n, [](Punct2DColorat dot) { return dot.get_color() != NULL; });

    for (int i = 0; i < n; i++)
    {
        std::cout << dots[i];
        if (dots[i].get_color() != NULL)
        {
            std::cout << "Culoare :" << dots[i].get_color() << "\n";
        }
        std::cout << "\n";
    }
} 

我想用颜色对点进行排序!= NULL,我试过这个,它有效,但是我有运行时错误。

bool Punct2DColorat::operator<(Punct2DColorat dot)
{
    if ((this->color != NULL) && (dot.get_color() != NULL))return this->x < dot.get_x();
    return true;
} 

如何只对具有颜色的对象进行排序!= NULL,其他颜色== NULL的对象保持在同一位置?

以下是一个例子:

//If have 3 objects in the following order stored in the dots array.
dots[0].get_x()=3;
dots[0].get_y()=3;
dots[0].get_color()="Red";

dots[1].get_x()=0;
dots[1].get_y()=0;
dots[1].get_color()=NULL;

dots[2].get_x()=1;
dots[2].get_y()=1;
dots[2].get_color()="Blue";

//After sort i want to have them like this:
dots[0].get_x()=1;
dots[0].get_y()=1;
dots[0].get_color()="Blue";

dots[1].get_x()=0;
dots[1].get_y()=0;
dots[1].get_color()=NULL;

dots[2].get_x()=3;
dots[2].get_y()=3;
dots[2].get_color()="Red";

感谢。

1 个答案:

答案 0 :(得分:4)

问题是,对于任何一些非彩色点,您的比较运算符的计算结果为true。 一种可能的解决方案是构造第二个向量,对其进行排序并重新插入

std::vector<Punct2DColorat> tmp;
for (int i = 0; i < n; i++)
{
    if (dots[i].get_color() != NULL)
    {
        tmp.push_back(dots[i]);
    }
}
std::sort(tmp.begin(), tmp.end());
int j = 0;
for (int i = 0; i < n; i++)
{
    if (dots[i].get_color() != NULL)
    {
        dots[i] = tmp[j];
        ++j;
    }
}