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

标签 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";
    }
} 

我想用 color !=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";

谢谢。

最佳答案

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

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;
    }
}

关于c++ - 如何使用 std::sort 对数组中的特定对象进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44084437/

相关文章:

c++ - 如何检查简单的 C++ 标准库迭代器是否可以被推进?

c++ - 如何使用 g++6 使用 std::reduce 和 #include <execution_policy> 编译代码?

c++ - 它用什么样的二叉搜索树来实现 `std::set` ?

c++ - 即使从未使用过,递减 std::vector::begin 是否未定义?

c++ - LeakSanitizer : get run time leak reports?

C++ 17 友元函数声明和内联命名空间

c++ - 在迭代期间调用集合上的 erase()

c++ - 使用 auto 作为迭代器

c++ - 基于节点的容器的最佳分配器?

c++ - 为什么 SGI STL 不使用 copy-and-swap 习惯用法?