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

在 Visual C++ 演示如何使用 、向量、双端队列:: operator==、向量、双端队列:: operatorAMP_LT 标准 (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== 比较模板类、向量、双端队列两个对象。 函数返回 左侧范围 == 权限范围(等于启动,。结束权限启动)。 为相等,元素数必须是相等的两、向量、双端队列对象。 第二个模板函数重载 operatorAMP_LT 比较模板类、向量、双端队列两个对象。 函数返回:( lexicographical_compare启动,。结束权限启动权限结束)。 由于使用 lexicographical_compare ,元素的数目无关紧要,在使用 operatorAMP_LT时。 代码示例中,添加一行代码,当创建 b 对象,例如 *b.*push_front时 ("D"); ,与 *a.*将使 b

示例

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

请参见

概念

标准模板库示例