Forum
 
ForumForumDiscussions and...Discussions and...C++C++Understandable writing?Understandable writing?
Disabled 
Previous
 
Next Next
New Post
 17/03/2008 08:55
 

Dear all,

In interviews with several people we recently had, I asked to read aloud and explain the following C++ code excerpt:

const int function_name( value_type const x )
{
   return std::accumulate
   ( &x      // a.
   , &x + 1  // b.
   , 0       // c.
   , detail::count_function< value_type >() // d.
   );
}

My intention was to write code that is easy to understand by companion C++ programmers. However, to my surprise, not one could explain the fragment, even though they knew about the STL and could tell that it comprises containers and algorithms, then again failing to mention that iterators connect those.

Having the detail::count_function class template already available, I preferred to write the above instead of:

const int function_name( value_type const x )
{
   return detail::count_function< value_type >()( 0, x );
}

Completely opposite to my aim, the solution that uses std::accumulate was criticized as being unreadable. Of course, the names are chosen to be a little unhelpful in the interview situation, nevertheless I really hope you can guess that function_name may be about counting bits (set or cleared) in one object of type value_type and that detail::count_function() is a function object suitable for std::accumulate.

template< typename T >
class count_function : public std::binary_function<T, T, int>;

Then, this only is quite, ehm standard use of an algorithm. It appears that STL has not completely entered every C++ programmer's vocabulary yet. The containers are appreciated, algorithms fall behind.

Years ago, Kevlin Henney, already explored what happens when you apply a generic programming approach to string management in C++ in The Next Best String. It's all about standard algorithms used with std::string. If you've not yet read something by Kevlin, it's well worth it, he is a gifted writer of code and prose.

In his book Extended STL, Volume 1: Collections and Iterators, Matthew Wilson takes the container--algorithm--iterator concept even further: Containers broaden to collections, such as a file system, the registry and character streams.

As an exercise I wrote the WordIndex program that contains a tokeniser (or tokenizer if you're not in the UK) that provides an iterator interface. Reading from the text input stream is simply written as follows:

std::ostream& operator<< ( std::ostream& os, Tokenizer::value_type const value )
{
   return os << "(" << value.first << "," << value.second << ")";
}

void copy_tokens( std::istream& is, std::ostream& os )
{
   Tokenizer tokenizer( is );

   std::copy
   ( tokenizer.begin()              // input collection begin
   , tokenizer.end()                // input collection end
   , std::ostream_iterator<Tokenizer::value_type>( os, "\n" )
   );
}

int main()
{
   copy_tokens( std::cin, std::cout );
}

Many, many kinds of collections are awaiting to have algorithms applied to them.

Happy writing!

Martin.
___

Resources:

  1. Kevlin Henney.  The Next Best String (PDF, 76KB), C++ Workshop column in Application Development Advisor 6(8), October 2002.
  2. Matthew Wilson. Extended STL, Volume 1: Collections and Iterators. Addison–Wesley Professional, 2007. ISBN-10 0–321–30550–7, ISBN-13 978–0-321–30550–3.
  3. Martin Moene. WordIndex: create a linenumber cross-referenced list of words.
Disabled 
Previous
 
Next Next
ForumForumDiscussions and...Discussions and...C++C++Understandable writing?Understandable writing?

Forum Usage Guidelines

The forums are a place for all to exchange ideas and techniques, and to post and answer questions.  All are welcome to read, registration is required to post. 

If you learn somthing new, discover or acquire a new technique, then please take a moment to register and rate the post that just helped you.  This site does not send spam and it does not release your personal details.  Full details in the site privacy policy.

We have some simple posting guidelines to keep the forums a pleasant and informative environment.

  • No flames, no trolls
  • No profanity, no racism
  • Site management has the final word on approving or removing any thread, post, or comment
  • English language only please

 

Copyright 2002-15 by Dynamisys Ltd