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:

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

I’d much rather write this:

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

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

template
typename Container::const_iterator find ( const Container & c, const T& value )
{
	return std::find(c.begin(), c.end(), value);
}

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