16 Mayıs 2019 Perşembe

std::greater Yapısı

Giriş
Bu sınıfın tersi std::less. std::lesser değil! ki zaten böyle bir kelime yok. Açıklaması şöyle.
In English, we say "greater than" or "less than", not "lesser than" (examples: "5 is greater than 4" and "4 is less than 5"). I don't have any evidence to back my next claim up, but I think it's reasonable to conclude that these type names were chosen based on the common English phrasing.
operator bool metodu
İmzası şöyle.
constexpr bool operator()( const T& lhs, const T& rhs ) const;
İmzada const olduğu için bu yapıyı std::sort() ile kullanmak istersem kendi sınıfımın > metodu da const olmalı. Şöyle yaparız
bool operator > (const MyStruct& other) const {
  return (key > other.key);
}
Örnek
C++11 ile şöyle yaparız.
sort (arr, arr + N, std::greater<int>{});
C++14 ile argument type geçmeye gerek yok şöyle yaparız.
sort (arr, arr + N, std::greater<>{});
C++17 ile <> karakterlerine de gerek yok şöyle yaparız.
sort (arr, arr + N, std::greater{});




Hiç yorum yok:

Yorum Gönder