Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
abs
Expand Minimize
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

deque::operator== and deque::operator< 

Illustrates how to use the deque::operator== and deque::operator< Standard Template Library (STL) functions in Visual C++.


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
   );

NoteNote

The class/parameter names in the prototype do not match the version in the header file. Some have been modified to improve readability.

The first template function overloads operator== to compare two objects of template class deque. The function returns Left.size == Right.size && equal(Left.begin, Left.end, Right.begin). For equality, the number of elements must be equal in both deque objects. The second template function overloads operator< to compare two objects of template class deque. The function returns: lexicographical_compare(Left.begin, Left.end, Right.begin, Right.end). Because lexicographical_compare is used, the number of elements does not matter while using operator<. In the sample code, adding a line of code while creating the b object, such as b.push_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;
}

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

Header: <deque>

Community Additions

ADD
Show:
© 2015 Microsoft