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.
bool color::operator<(const color & other) const
{
return(this->blue < other.blue , this->red < other.red , this->green<other.green);
}
Teraz dla odmiany napisz jaki masz problem i co wg ciebie ten kod powinnien zwrócić?
chce zeby operator < sprawdzal czy dany kolor (r,g,b) wystepuje w kontenerze
Pokaż klase kolor, jak Tworzysz container, i jak operator <
ma sprawdzać czy obiekt jest w zbiorze? Może ==
?
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.
unsigned color::rgb()const { return ((((0U|r)<<8)|g)<<8)|b; }
bool color::operator<(const color &other)const { return this->rgb()<other.rgb(); }
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>;
Nie bardzo rozumiem dlaczego OP usunął swoje konto. Czy to był ban?