Kontener set

0

Mój problem polega na tym , że stworzyłem sobie kontener set do którego wpisuje obiekty klasy kolor . W tym kontenerze mają znajdować się unikatowe kolory (bez powtórzeń) . Mam problem z przeciazeniem operatora < (bool color::operator<(const color & other) const)
Kolor posiada skladowe red,green.blue.

0

bool color::operator<(const color & other) const
{
return(this->blue < other.blue , this->red < other.red , this->green<other.green);
}

0

Teraz dla odmiany napisz jaki masz problem i co wg ciebie ten kod powinnien zwrócić?

0

chce zeby operator < sprawdzal czy dany kolor (r,g,b) wystepuje w kontenerze

0

Pokaż klase kolor, jak Tworzysz container, i jak operator < ma sprawdzać czy obiekt jest w zbiorze? Może ==?

0
bool color::operator<(const color & other) const
{
return(this->blue < other.blue , this->red < other.red , this->green<other.green);
}

To jest równoważne temu zapisowi:

bool color::operator<(const color & other) const
{
return this->green<other.green;
}

https://en.cppreference.com/w/cpp/language/operator_other#Built-in_comma_operator
Musisz inaczej skonstruować ten warunek.

1
unsigned color::rgb()const { return ((((0U|r)<<8)|g)<<8)|b; }
bool color::operator<(const color &other)const { return this->rgb()<other.rgb(); }
1

Jako, że klasa opisująca kolor nie mia jednoznacznego porządku, to dostarczanie operatora < i korzystanie z kontenera std::set jest niewskazane (dziwne).
IMO prościej jest skorzystać z std::unordered_set i dostarczyć hash koloru (co jest banalnie proste, choćby stosując metodę wskazaną przez _13th_Dragon).

struct color_hash {
    std::size_t operator()(color c) const noexcept {
        return static_cast<size_t>(c.red()) + static_cast<size_t>(c.green()) << 8 + static_cast<size_t>(c.blue()) << 16;
    }
};

using color_set = std::unordered_set<color, color_hash>;
0

Nie bardzo rozumiem dlaczego OP usunął swoje konto. Czy to był ban?

1 użytkowników online, w tym zalogowanych: 0, gości: 1