続いては比較演算子のオーバーロードについて説明します。
比較演算子は「==」「!=」「<」「>」「<=」「>=」です。
比較した結果はbool型で返します。
比較演算子はクラス同士の大小比較などに用いられるため、クラスのソートなどでも役立ちます。
まずは、基本的な使い方をざっと書きましょう。
<sample program cpp066-01>
#include <iostream> class Sample { public: Sample(const int value); bool operator==(const Sample &rhs) const; bool operator!=(const Sample &rhs) const; bool operator<(const Sample &rhs) const; bool operator>(const Sample &rhs) const; bool operator<=(const Sample &rhs) const; bool operator>=(const Sample &rhs) const; private: int m_value; }; int main() { Sample sample1(5); Sample sample2(8); if (sample1 == sample2) { std::cout << "sample1 == sample2" << std::endl; } if (sample1 != sample2) { std::cout << "sample1 != sample2" << std::endl; } if (sample1 < sample2) { std::cout << "sample1 < sample2" << std::endl; } if (sample1 > sample2) { std::cout << "sample1 > sample2" << std::endl; } if (sample1 <= sample2) { std::cout << "sample1 <= sample2" << std::endl; } if (sample1 >= sample2) { std::cout << "sample1 >= sample2" << std::endl; } return 0; } Sample::Sample(const int value) : m_value(value) { } bool Sample::operator==(const Sample &rhs) const { return m_value == rhs.m_value; } bool Sample::operator!=(const Sample &rhs) const { return m_value != rhs.m_value; } bool Sample::operator<(const Sample &rhs) const { return m_value < rhs.m_value; } bool Sample::operator>(const Sample &rhs) const { return m_value > rhs.m_value; } bool Sample::operator<=(const Sample &rhs) const { return m_value <= rhs.m_value; } bool Sample::operator>=(const Sample &rhs) const { return m_value >= rhs.m_value; } |
<実行結果>
sample1 != sample2 sample1 < sample2 sample1 <= sample2 続行するには何かキーを押してください・・・
比較するだけなので、関数の後ろに「const」を付けます。
今回は全部作ってみましたが、必要なものだけ用意すれば十分だと思います。
何か意味のあるプログラムを作ってみましょう。
上にも書いたクラスのソートを考えてみます。
例えば、あるクラスがあり「ベクタ」に入っていたとします。
それをアルゴリズムのソートを使って並べ替えをしたいと思います。
まずは、ベースになるプログラムを作ります。
<sample program cpp066-02>
#include <iostream> #include <vector> #include <cstdlib> #include <ctime> static const int COUNT = 5; static const int MAX_RAND = 50; class Sample { public: Sample(const int value); void Show() const; private: int m_value; }; int main() { srand((unsigned int)time(NULL)); std::vector<Sample> vecSample; for (int i = 0; i < COUNT; i++) { Sample work(rand() % MAX_RAND); vecSample.push_back(work); } for (int i = 0; i < COUNT; i++) { vecSample[i].Show(); } return 0; } Sample::Sample(const int value) : m_value(value) { } void Sample::Show() const { std::cout << m_value << std::endl; } |
<実行結果>
33 28 13 21 37 続行するには何かキーを押してください・・・
Sampleクラスはメンバ変数m_valueを1つ持っています。
引数を持つコンストラクタ関数で値をセットし、Show関数で中身が表示出来ます。
main関数でSampleクラスの「ベクタ」を用意し、乱数を使って5個のデータを格納しています。
とりあえず、5つのデータ(クラス)のメンバ変数の中身を表示して終わりです。
さて、これをソートしたいと思います。
上にも書いた通り、ソートは標準テンプレートライブラリにあるものを使います。
<sample program cpp066-03>
#include <iostream> #include <vector> #include <algorithm> #include <cstdlib> #include <ctime> static const int COUNT = 5; static const int MAX_RAND = 50; class Sample { public: Sample(const int value); void Show() const; private: int m_value; }; int main() { srand((unsigned int)time(NULL)); std::vector<Sample> vecSample; for (int i = 0; i < COUNT; i++) { Sample work(rand() % MAX_RAND); vecSample.push_back(work); } std::sort(vecSample.begin(), vecSample.end()); for (int i = 0; i < COUNT; i++) { vecSample[i].Show(); } return 0; } Sample::Sample(const int value) : m_value(value) { } void Sample::Show() const { std::cout << m_value << std::endl; } |
<コンパイル結果>
error C2672: 'operator __surrogate_func': 一致するオーバーロードされた関数が見つかりませんでした。 error C2893: 関数テンプレート 'unknown-type std::less'<void>::operator ()(_Ty1 &&,_Ty2 &&) const' の特定に失敗しました
エラーが沢山表示されました・・・
多すぎるので大事なところだけ抜き出して書いてあります。
大事なところは、
std::less'<void>::operator ()(_Ty1 &&,_Ty2 &&) const' の特定に失敗しました |
このメッセージです。
中に書いてある「less」とは「<」の事です。
ソートを行うには大小比較が必須ですが、クラスに大小比較をする手段が用意されていないのでエラーになっています。
そこで、演算子オーバーロードの出番です。
「<」演算子をオーバーロードしてみましょう。
<sample program cpp066-04>
#include <iostream> #include <vector> #include <algorithm> #include <cstdlib> #include <ctime> static const int COUNT = 5; static const int MAX_RAND = 50; class Sample { public: Sample(const int value); void Show() const; bool operator<(const Sample &rhs); private: int m_value; }; int main() { srand((unsigned int)time(NULL)); std::vector<Sample> vecSample; for (int i = 0; i < COUNT; i++) { Sample work(rand() % MAX_RAND); vecSample.push_back(work); } std::sort(vecSample.begin(), vecSample.end()); for (int i = 0; i < COUNT; i++) { vecSample[i].Show(); } return 0; } Sample::Sample(const int value) : m_value(value) { } void Sample::Show() const { std::cout << m_value << std::endl; } bool Sample::operator<(const Sample &rhs) { return m_value < rhs.m_value; } |
<実行結果>
6 10 17 34 39 続行するには何かキーを押してください・・・
ソート出来ました!
他のアルゴリズムでもクラスを使う場合、大小比較が必要な場合があります。
その時には演算子オーバーロードを使ってみてください。