Wrapping STL algorithms for use with STL containers

In the algorithm header, most algorithms work on iterator ranges.  Most of the time, I just want the algorithm to work on the whole container.  So, for example, with std::find I'll often find myself typing this:

  1. location = std::find(mycontainer.begin(), mycontainer.end(), somevalue);

I'd much rather write this:

  1. location = std::find(mycontainer, somevalue);

So I started wrapping the STL algorithms to work on containers, like so:

  1. template<class Container, class T>
  2. typename Container::const_iterator find ( const Container & c, const T& value )
  3. {
  4. return std::find(c.begin(), c.end(), value);
  5. }

Nice and easy. I just put that into a namespace called calgo, and then can finally use a simpler syntax!

Comments are closed.

Dev Journal and Project Hosting