次の方法で共有


deque::operator== と deque::operator<

Visual C++ で deque:: operator==deque:: operator< の標準テンプレート ライブラリ関数を使用する方法に (STL) ついて説明します。

template<class T, class A>
   bool operator==(
      const deque <T, A>& Left,
      const deque <T, A>& Right
   );
template<class T, class A>
   bool operator<(
      const deque <T, A>& Left,
      const deque <T, A>& Right
   );

解説

[!メモ]

プロトタイプのクラスやパラメーター名はヘッダー ファイルのバージョンと一致しない。ただし読みやすさが向上するように変更されました。

一つ目のテンプレート関数はdeque 2 のテンプレート クラスの二つのオブジェクトを比較するに operator== にオーバーロードされます。 残りの 関数を返します。 サイズ == のアクセス許可。 サイズ Equals( 似ています残り begin終了 のアクセス許可。begin)。等価の場合は要素の数は両方の deque のオブジェクトである必要があります。2 番目のテンプレート関数はdeque 2 のテンプレート クラスの二つのオブジェクトを比較するに operator< にオーバーロードされます。関数の戻り値 : lexicographical_compare( 似ています残り begin終了 のアクセス許可。begin のアクセス許可。 終了 )。lexicographical_compare が使用されるため要素の数は operator< を使用すると重要ではありません。コード行を追加してサンプル コードでは push_frontb.("D") などの b の オブジェクトを作成中に ; ab. 大きくなります

使用例

// deque_operators.cpp
// compile with: /EHsc
//
// Functions:
//    ==
//    <

#include <iostream>
#include <deque>

using namespace std;

typedef deque<char >  CHARDEQUE;
void print_contents (CHARDEQUE  deque, char*);

int main()
{
    //create a  with  3 A's
    CHARDEQUE  a(3,'A');
    a.push_front('C');

    //create b with 4 B's.
    CHARDEQUE  b(6,'B');

    //print out the contents
    print_contents (a,"a");
    print_contents (b,"b");

    //compare a and b
    if (a==b)
        cout <<"a is equal to b"<<endl;
    else if(a<b)
            cout <<"a is less than b"<<endl;
    else
        cout <<"a is greater than b" <<endl;

    //assign the contents of b to a
    a.assign(b.begin(),b.end());
    print_contents (a,"a");
    print_contents (b,"b");

    //compare a and b again
    if (a==b)
        cout <<"a is equal to b"<<endl;
    else if(a<b)
            cout <<"a is less than b"<<endl;
    else
        cout <<"a is greater than b" <<endl;

}

//function to print the contents of deque
void print_contents (CHARDEQUE  deque, char *name)
{
    CHARDEQUE::iterator pdeque;

    cout <<"The contents of "<< name <<" : ";

        for(pdeque = deque.begin();
        pdeque != deque.end();
        pdeque++)
    {
        cout << *pdeque <<" " ;
    }
        cout<<endl;
}

出力

The contents of a : C A A A 
The contents of b : B B B B B B 
a is greater than b
The contents of a : B B B B B B 
The contents of b : B B B B B B 
a is equal to b

必要条件

ヘッダー : <deque>

参照

概念

標準テンプレート ライブラリのサンプル