共用方式為


deque::operator== 和 deque::operator<

說明如何使用 deque::operator = =deque::operator < Visual C++ 標準樣板程式庫 (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
   );

備註

注意事項注意事項

在原型中的類別/參數名稱不相符的標頭檔中的版本。某些已修改以提高可讀性。

第一個樣板的函式多載operator==來比較兩個物件的範本類別的 deque。函式會傳回size == Right.大小 & & equal(Left.開始, Left.end, Right.begin).相等,項目數目必須在這兩個 deque 物件相等。第二個樣板的函式多載運算子 < 來比較兩個物件的範本類別的 deque。此函式傳回: lexicographical_compare(開始, Left.end, Right.begin, Right.end).因為lexicographical_compare是使用元素數目不重要時使用運算子 <。在範例程式碼中,請加入一行程式碼建立時 b 物件,例如 bpush_front('D');, will make b greater than a.

範例

// 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;
}

Output

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>

請參閱

概念

標準樣板程式庫範例