STL Helper Algorithms


Feedback from our customers concerning the ease of using STL is encouraging. After a brief period of adjustment to the new philosophy, users enjoy the new power. However, there are some concerns about certain aspects of usability that are worth discussing.

The main concern centers around the possible misuse of algorithms. For example, although the count() algorithm expects iterators that reference the same container, there is nothing to stop iterators from referencing different containers.

	vector< int > v1( 10 );
  	vector< int > v2( 10 );
  	// ... fill v1 and v2 with values.
  	int n; // Oops, n should be initialized to zero!
  	count( v1.begin(), v2.end(), 42, n ); // Oops!
	

One possible way to avoid this is to add a new derived class to each STL container containing user-friendly versions of the most common algorithms, as shown in the following example.

	template< class T >
  	class non_standard_vector : public vector< T >
    	{
    	public:
      	int count( const T& value_ ) const;
	 };
	 

Using this new class you can write the following code.

	non_standard_vector< int > v1( 10 ); // NON-STANDARD VECTOR!
  	int n = v1.count( 42 ); // Easier.
	

An alternative approach is to encapsulate every STL container in a non-standard and easier-to-use version, as in the following example.

	template< class T >
  	class non_standard_vector
    	{
    	public:
       	<easier to use interface goes here>
    	private:
      	vector< T > implementation; // Encapsulated standard version.
    	};
	

Although these approaches seem reasonable at first glance, Recursion Software rejected these solutions for the following reasons.

In other words, both of the above approaches are contrary to the original aims of the STL design.

The Recursion Software solution, on the other hand, has less long-term drawbacks and is consistent with the generic programming style of STL itself. Helper<ToolKit> includes additional helper algorithms that achieve the same goal as the specialized derived class approach. Helper<ToolKit> achieves this without requiring use of non-standard classes, as shown in the code fragment below.

	vector< int > v1( 10 ); // Standard vector!
  	int n = os_count( v1, 42 ); // Easiest!
	

The additional os_count() algorithm does some of the work a programmer would otherwise have to do manually, but is just as efficient as the original.

The significance of the helper algorithms becomes more apparent when an operation such as removing items from a container is considered. Without the helper algorithms, the following code is required to remove all of the 42s from the vector v .

	v.erase( remove( v.begin(), v.end(), 42 ), v.end() );
	

Using the os_erase() helper algorithm, you can write the following code instead.

	os_erase( v, 42 );
	

Helper<ToolKit> defines helpers for the following Standard Template Library algorithms in <ospace/helper.h>.
 
  • os_binary_search
  • os_count
  • os_count_if
  • os_erase
  • os_erase_if
  • os_find
  • os_find_if
  • os_for_each
  • os_includes
  • os_max_element
  • os_min_element
  • os_random_shuffle
  • os_release
  • os_replace
  • os_replace_if
  • os_rotate
  • os_sort

Helpers for regular expression and string support algorithms are discussed in detail in the following sections of this chapter.


Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.