Next: High Integrity CPP Rule 17.20  Up: 17 Standard Template Library (STL)  Previous: High Integrity CPP Guideline 17.18  Contents

High Integrity CPP Rule 17.19   Use container member functions rather than algorithms with the same name.

Justification

Where particular container operations are implemented as members these members should be used instead of the generic algorithm. Member implementations can take advantage of internal container structure and this leads to a more efficient implementation.

   
   void foo( std::set< int >& s )
   {
      std::set< int >::iterator iter;
   
      // Generally std::find cannot take advantage of the structure
      // of the container it operates on and executes with linear
      // complexity.
      //
      iter = std::find( s.begin(), s.end(), 10 );
   
      // set.find takes advantage of the structure of the set and
      // executes with logarithmic complexity.
      //
      iter = s.find( 10 );
   }
See also

Guideline 17.18

Reference

Effective STL Item 44;


HICPP VERSION 2.4  http://www.codingstandard.com   Copyright: © 2007 THE PROGRAMMING RESEARCH GROUP