Microsoft KB Archive/156790

= How to use the operator == and the operator < Standard Template Library (STL) functions for the deque container class in Visual C++ =

Article ID: 156790

Article Last Modified on 1/11/2006

-

APPLIES TO


 * Microsoft Visual C++ 4.2 Enterprise Edition
 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 4.2 Professional Edition
 * Microsoft Visual C++ 5.0 Professional Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition
 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-



This article was previously published under Q156790



Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code. Microsoft Visual C++ 2005 supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model.



SUMMARY
The sample code below illustrates how to use the operator == and the operator < STL functions for the deque container class in Visual C++.



Prototype
template

bool operator==(          const deque & lhs,           const deque & rhs);

template

bool operator<(          const deque & lhs,           const deque & rhs); NOTE: The class/parameter names in the prototype may not match the version in the header file. Some have been modified to improve readability.

Description
The first template function overloads operator== to compare two objects of template class deque. The function returns: lhs.size == rhs.size && equal(lhs. begin, lhs. end,  rhs.begin). For equality, the number of elements must be equal in both the deque objects.

The second template function overloads operator< to compare two objects of template class deque. The function returns: lexicographical_compare(lhs. begin, lhs. end, rhs.begin,  rhs.end). Because lexicographic (as in a dictionary) 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.

Sample Code
//////////////////////////////////////////////////////////////////////   //    // Compile options needed: -GX //   // deque.cpp : //   // Functions: //   //    ==   //    <   //    // Written by Bobby Mattappally // of Microsoft Product Support Services, // Copyright (c) 1996 Microsoft Corporation. All rights reserved. //////////////////////////////////////////////////////////////////////

/* Compile options needed:-GX */   #include #include using namespace std; // std c++ libs implemented in std #endif
 * 1) if _MSC_VER > 1020  // if VC++ version is > 4.2

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

void 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"<  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

