【C++】std::compareで比較を簡単に行う【標準ライブラリ】

【C++】std::compareで比較を簡単に行う【標準ライブラリ】

std::compareとは

std::compareは、C++20で導入された比較を簡単に行うための型です。この型は、主に2つのオペランドを比較する際の結果を表現します。

std::compareは、従来の比較関数や比較演算子をシンプルに扱うために設計されており、比較結果として3つの状態を持ちます:

  • std::strong_ordering::less
  • std::strong_ordering::equal
  • std::strong_ordering::greater

これにより、比較結果が直感的に理解できるようになります。

std::compareの機能

std::compareにはいくつかの重要な機能があります。主に2つの大きな目的に使われます:

  • 二つの値の比較を行う
  • 比較結果を比較する(例えば、複雑なオブジェクトの比較など)

std::compareは、`operator<`、`operator==`、`operator>`といった演算子を簡素化し、どの順序の比較を行ったかを管理します。また、これらの演算子において出力されるのは、`std::strong_ordering`型のオブジェクトです。

強い順序の比較

std::compareが提供する「強い順序」の比較では、各オペランドが単純に大小、等号で比較されます。この比較は、高度に最適化されており、簡単に利用できます。

例えば、`std::strong_ordering`を利用した`<`、`>`演算子を見てみましょう:

        #include 
        int main() {
            int a = 5;
            int b = 10;
            std::strong_ordering result = (a <=> b);
            if (result == std::strong_ordering::less) {
                std::cout << "a is less than b" << std::endl;
            }
            return 0;
        }
        

この例では、`a`と`b`を比較して、aがbより小さい場合には"less"を表示します。

std::compareを使った比較の例

次に、いくつかの比較の例を見ていきましょう。

基本的な比較

まず、簡単な整数の比較から始めます:

        #include 
        #include 

        int main() {
            int x = 3, y = 5;
            auto result = (x <=> y);
            
            if (result == std::strong_ordering::less) {
                std::cout << "x is less than y" << std::endl;
            } else if (result == std::strong_ordering::equal) {
                std::cout << "x is equal to y" << std::endl;
            } else {
                std::cout << "x is greater than y" << std::endl;
            }
            return 0;
        }
        

上記のコードでは、`x`と`y`を比較し、それぞれの場合に応じた結果を出力します。

文字列の比較

次に、文字列を比較する例です。C++の`std::string`に対しても、`std::compare`を使用することができます:

        #include 
        #include 
        #include 

        int main() {
            std::string s1 = "apple", s2 = "banana";
            auto result = (s1 <=> s2);
            
            if (result == std::strong_ordering::less) {
                std::cout << "s1 is less than s2" << std::endl;
            } else if (result == std::strong_ordering::equal) {
                std::cout << "s1 is equal to s2" << std::endl;
            } else {
                std::cout << "s1 is greater than s2" << std::endl;
            }
            return 0;
        }
        

このコードは、2つの文字列を比較して、その大小を判定します。

ユーザー定義型の比較

`std::compare`は、ユーザー定義型(例えば、構造体やクラス)でも使用することができます。以下の例では、`Point`クラスを定義し、その比較を行います:

        #include 
        #include 

        struct Point {
            int x, y;
            auto operator<=>(const Point&) const = default;
        };

        int main() {
            Point p1 = {1, 2}, p2 = {2, 3};
            auto result = (p1 <=> p2);

            if (result == std::strong_ordering::less) {
                std::cout << "p1 is less than p2" << std::endl;
            } else if (result == std::strong_ordering::equal) {
                std::cout << "p1 is equal to p2" << std::endl;
            } else {
                std::cout << "p1 is greater than p2" << std::endl;
            }
            return 0;
        }
        

この例では、`Point`クラスに対して`operator<=>`を使って比較を行い、2つの`Point`オブジェクトの大小を判定しています。

まとめ

std::compareは、C++20で導入された新しい比較手法であり、比較演算子の使用を簡素化し、強い順序に基づく比較結果を返します。これにより、比較を行う際に一貫性が保たれ、コードの可読性が向上します。

また、`std::compare`は整数、文字列、さらにはユーザー定義型に至るまで幅広く使用することができ、複雑な比較を簡潔に書くことができます。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です