I l@ve RuBoard Previous Section Next Section

Item 7. Using the Standard Library (or, Temporaries Revisited)

Difficulty: 5

Effective reuse is an important part of good software engineering. To demonstrate how much better off you can be by using standard library algorithms instead of handcrafting your own, let's reconsider the previous Item to demonstrate how many of the problems could have been avoided by simply reusing what's already available in the standard library.

How many pitfalls in Item 6 could have been avoided if only the programmer had used a standard library algorithm instead of handcrafting the loop? Demonstrate. (As with Item 6, don't change the semantics of the function, even though they could be improved.)

To recap, here is the mostly-fixed function:



string FindAddr( const list<Employee>& emps, 


                 const string&         name )


{


  list<Employee>::const_iterator end( emps.end() );


  for( list<Employee>::const_iterator i = emps.begin();


       i != end;


       ++i )


  {


    if( i->name == name )


    {


      return i->addr;


    }


  }


  return "";


}


    I l@ve RuBoard Previous Section Next Section