home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 33,018 of 33,346   
   Daryle Walker to All   
   Implementing operator<, std::mismatch vs   
   06 May 13 09:37:20   
   
   From: darylew@googlemail.com   
      
   I implemented operator== for my container type with std::equal.  I was   
   about to use std::lexicographical_compare for my operator<, but I   
   realized it may be inefficient:   
      
   1. Return special-case value if one or both lists end early.   
   2. You do *l < *r and return TRUE if this comparison does so.   
   3. Else, do *r++ < *l++ and return FALSE if this comparison returns   
      true.   
   4. Otherwise, you're currently equal and go back to (1).   
      
   The lexicographical compare makes its decision at the first unequal   
   pair of elements (or FALSE if all are equal).  Instead of two calls to   
   operator< to determine equality, just use one call to operator==, then   
   return the comparison at the mismatch point.   
      
   Now, I know this plan would require carrying two different comparators   
   for the non-operator variants (and hoping that their comparison   
   philosophies are in sync); that's probably why the standard versions   
   just call operator< twice.  But for my private code, where I pre-   
   condition the element type to have both operators, would it be better   
   to just use std::mismatch & one operator< call instead of   
   std::lexicographical_compare?   
      
   (BTW, my structure has all containers have the same size, so I don't   
   do that check.)   
      
   Daryle W.   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca