sequence_algorithm.hpp

sequence_algorithm.hpp provides a convenient interface to the algorithms defined in the C++ STL algorithm header file: Rather than specify sequences by two arguments (viz. begin and end iterators), a sequence is specified with just one argument.

Instead of writing:

   std::for_each(mySTL_Container.begin(), mySTL_Container.end(), do_it);

with sequence_algorithm.hpp #include'ed it's possible to write:

   for_each(mySTL_Container, do_it);

The statement using the for_each defined in sequence_algorithm.hpp is easier to comprehend and easier to write with little chance of making a mistake.

According to section 18.3 of "The C+ Programming Language" (third edition) by Bjarne Stroustrup the STL algorithms were defined to take begin and end iterators instead of sequences for the sake of "generality". However, I see no loss of generality defining the STL algorithms in terms of sequences: A simple "adapter" class can always be used to turn a pair of iterators into a sequence and more often than not the algorithms can be used without such an adapter.

In addition to the STL algorithms, sequence_algorithm.hpp defines copy_if and an overloaded version of transform that overwrites its input sequence (this function doesn't take a begin iterator for the output).

Download

sequence_algorithm.hpp
sequence.hpp



Last update: 24 December 2008