2017-05-20 6 views
2

私はこれらのクラスを持っており、x座標を考慮してオブジェクトの配列をソートし、特定の値を持つものだけを属性にソートします。std :: sortを使用して配列から特定のオブジェクトをソートする方法は?

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

ありがとう:ここ

は一例です。

+0

'void main'が間違っている。 'main'は' int'を返さなければなりません。 – melpomene

+0

サンプルの見た目はどのようにソートされるべきですか? –

+0

mainから返された型を変更しただけで、何も変更されませんでした。 –

答えて

5

問題は、比較演算子は、色の付いていないいくつかの点について真と評価することです。 可能な解決策は、第2のベクトルを構成し、ソートして再挿入することである。

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

私はこれであなたの解決策は素晴らしいとは思わなかった。 –

関連する問題