beautypg.com

Tips and techniques for building algorithms, The iterator_category primitive – HP Integrity NonStop H-Series User Manual

Page 216

background image

Click on the banner to return to the user guide home page.

©Copyright 1996 Rogue Wave Software

Tips and Techniques for Building Algorithms

This sections describes some techniques that use features of iterators to increase the flexibility
and efficiency of your algorithms.

The iterator_category Primitive

Sometimes an algorithm that can be implemented most efficiently with a random access iterator
can also work with less powerful iterators. The Standard C++ Library includes primitives that
allow a single algorithm to provide several different implementations, depending upon the power
of the iterator passed into it. The following example demonstrates the usual technique for setting
up multiple versions of the same algorithm.

// Note, this requires that the iterators be derived from
// Standard base types, unless the iterators are simple pointers.

namespace my_namespace {

template
Iterator union(Iterator first1, Iterator last1,
Iterator first2, Iterator last2,
Iterator Result)
{
return union_aux(first1,last1,first2,last2,Result,
iterator_category(first1));
}

template
Iterator union_aux(Iterator first1, Iterator last1,
Iterator first2, Iterator last2,
Iterator Result, forward_iterator_tag)
{
// General but less efficient implementation
}

template

This manual is related to the following products: