Title: Generic Programming with the STL
1Generic Programming with the STL
- CS341
- Western Washington University
2What is Generic Programming?
- Its not Object-oriented
- It is a set of requirements on data types
- It is a programming style that emphasizes
defining abstract concepts and writing algorithms
and data structures in terms of abstract concepts - Generic algorithms are written by abstracting
algorithms on specific types and specific data
structures so that they apply to arguments whose
types are as general as possible - a generic algorithm is made up of the actual
instructions that describe the steps of the
algorithm - and the set of requirements that specify
precisely which properties its arguments types
must satisfy
3The STL(Standard Template Library)
- a formal object hierarchy of abstract
requirements that describe software components - created by Alex Stepanov, then of HP, now of SGI
- an example of generic programming
4A Simple Example
- int main()
-
- vectorltstringgt v
- string temp
- while (getline(cin, temp))
- v.push_back(temp)
- sort(v.begin(), v.end())
- copy(v.begin(), v.end(), ostream_iteratorltstringgt(
- cout, \n))
- return 0
5- This simple example reads in a vector of strings,
sorts them in ascending order, and prints them to
standard output - vector is the container class
- sort and copy are algorithms
- all STL algorithms take iterators as parameters
- an iterator can be anything from a C pointer to a
wrapper for an output stream - more on iterators to come
- this is a requirement of the argument types
6Concepts as part of the STL
- 3 different ways of understanding a concept
- first, it can be thought of as a list of type
requirements. - If a type T is a model of a concept C, then T
satisfies all of Cs requirements - second, a concept can be thought of as a set of
types - if a type T is a model of concept C, then type T
belongs to the set of types that T represents - third, a concept can be thought of as a set of
valid programs
7Basic Concepts
- Assignable
- xy and T x(y)
- Default Constructable
- T() and T t
- Equality Comparable
- x y
- LessThan Comparable
- x lt y and x gt y
- A regular type models Assignable, Default
Constructable, and Equality Comparable - most basic types are regular types
8More on Iterators
- They are a generalization of pointersbut they
are objects that point to objects - used to iterate over a range of values
- important part of generic programming because
they provide an interface between algorithms and
data structures
9Iterator Concepts
- Input Iterator
- Output Iterator
- Forward Iterator
- Bidirectional Iterator
- Random Access Iterator
Input Iterator
Output Iterator
Forward Iterator
Bidirectional Iterator
Random Access Iterator
10- Input iterator
- supports the following operations
- equality
- copy and assign
- dereferencing ()
- increment()
- it does not support
- you cannot necessarily modify what an Input
iterator points to - no guarantees decrementing, addition, or
subtraction will be successful - cannot compare for gt or lt
11- these are restrictions on algorithms that use
Input Iterators, not on types that model the
concept Input Iterator - some algorithms that require Input Iterators
include find, find_if, equal, partial_sum,
random_sample,and set_intersection - Input Iterator is essentially a single-pass
read-only pointer that behaves as if reading from
an input stream - An Example
- template ltclass InputIterator, class Tgt
- Iterator find(InputIterator first, InputIterator
last, const T value) -
- while(first ! last first !value)
- first
- return first
12Output Iterators
- An iterator that behaves as if writing to an
output stream - single pass, write-only iterator
- An example
- templateltclass InputIterator, class
OutputIteratorgt - OutputIterator copy(InputIterator first,
InputIterator last, OutputIterator result) -
- for ( first ! last result, first)
- resultfirst
13- Requirements
- copy and assign
- can use an OutputIterator to write a value px
is well-defined - can increment
- Restrictions
- single-pass
- write-only
- specify only a single boundary to define a range
- Algorithms that require OutputIterators
- copy, merge, transform
14Forward Iterators
- Address some of the limitations of InputIterators
and OutputIterators - read and write ability
- multi-pass
- may be constant or mutable
- An example
- template ltclass ForwardIterator, class Tgt
- void replace(ForwardIterator first,
ForwardIterator last, const T old_value, const
T new_value) -
- for ( first ! last, first)
-
- if (first old_value)
- first new_value
-
15Bidirectional Iterators
- Support motion in both directions
- allow increment and decrement
- Allow multi-pass algorithms, just like
ForwardIterators - May be constant or mutable
- An example
template ltclass BidirectionalIterator, class
OutputIteratorgt OutputIterator reverse_copy(Bidire
ctionalIterator first, BidirectionalIterator
last, OutputIterator result) while (first !
last) --last result last result
return result
16RandomAccessIterators
- RandomAccessIterators complete the set of
arithmetic operations defined for pointers - Input, Output, and Forward Iterators support
- Bidirectional Iterators support --
- RandomAccess Iterators also support pointer
arithmetic - p n p - n
- pn
- p1 - p2
- p1 lt p2
template ltclass RandomAccessIterator, class
Comparegt void sort(RandomAccessIterator
begin, RandomAccessIterator end, Compare comp)