• Print
This chapter is from the book

## 3.8 Algorithms

A data structure, such as a list or a vector, is not very useful on its own. To use one, we need operations for basic access such as adding and removing elements. Furthermore, we rarely just store objects in a container. We sort them, print them, extract subsets, remove elements, search for objects, etc. Consequently, the standard library provides the most common algorithms for containers in addition to providing the most common container types. For example, the following sorts a vector and places a copy of each unique vector element on a list:

```void f(vector<Entry>& ve, list<Entry>& le)
{
sort(ve.begin() ,ve.end()) ;
unique_copy(ve.begin() ,ve.end() ,le.begin()) ;
}```

The standard algorithms are expressed in terms of sequences of elements. A sequence is represented by a pair of iterators specifying the first element and the one-beyond-the-last element. In the example, sort() sorts the sequence from ve.begin() to ve.end()—which just happens to be all the elements of a vector. For writing, you need only to specify the first element to be written. If more than one element is written, the elements following that initial element will be overwritten.

If we wanted to add the new elements to the end of a container, we could have written this:

```void f(vector<Entry>& ve, list<Entry>& le)
{
sort(ve.begin() ,ve.end()) ;
unique_copy(ve.begin() ,ve.end() ,back_inserter(le)) ; // append to le
}```

A back_inserter() adds elements at the end of a container, extending the container to make room for them. Thus, the standard containers plus back_inserter()s eliminate the need to use error-prone, explicit C-style memory management using realloc(). Forgetting to use a back_inserter() when appending can lead to errors. For example:

```void f(vector<Entry>& ve, list<Entry>& le)
{
copy(ve.begin() ,ve.end() ,le) ;     // error: le not an iterator
copy(ve.begin() ,ve.end() ,le.end()) ;  // bad: writes beyond the end
copy(ve.begin() ,ve.end() ,le.begin()) ; // overwrite elements
}```

### 3.8.1 Use of Iterators

When you first encounter a container, a few iterators referring to useful elements can be obtained; begin() and end() are the best examples of this. In addition, many algorithms return iterators. For example, the standard algorithm find looks for a value in a sequence and returns an iterator to the element found. Using find, we can count the number of occurrences of a character in a string:

```int count(const string& s, char c) // count occurrences of c in s
{
int n = 0;
string: :const_iterator i = find(s.begin() ,s.end() ,c) ;
while (i != s.end()) {
++n;
i = find(i+1,s.end() ,c) ;
}
return n;
}```

The find algorithm returns an iterator to the first occurrence of a value in a sequence or the one-past-the-end iterator. Consider what happens for a simple call of count:

```void f()
{
string m = "Mary had a little lamb";
int a_count = count(m,'a') ;
}```

The first call to find() finds the a in Mary. Thus, the iterator points to that character and not to s.end(), so we enter the loop. In the loop, we start the search at i+1; that is, we start one past where we found the a. We then loop finding the other three a's. That done, find() reaches the end and returns s.end() so that the condition i!=s.end() fails and we exit the loop.

That call of count() could be graphically represented like this:

Figure 1

The arrows indicate the initial, intermediate, and final values of the iterator i.

Naturally, the find algorithm will work equivalently on every standard container. Consequently, we could generalize the count() function in the same way:

```template<class C, class T> int count(const C& v, T val)
{
typename C: :const_iterator i = find(v.begin() ,v.end() ,val) ;
int n = 0;
while (i != v.end()) {
++n;
++i;   // skip past the element we just found
i = find(i,v.end() ,val) ;
}
return n;
}```

This works, so we can say:

```void f(list<complex>& lc, vector<string>& vs, string s)
{
int i1 = count(lc,complex(1,3)) ;
int i2 = count(vs,"Diogenes") ;
int i3 = count(s,'x') ;
}```

However, we don't have to define a count template. Counting occurrences of an element is so generally useful that the standard library provides that algorithm. To be fully general, the standard library count takes a sequence as its argument, rather than a container, so we would say:

```void f(list<complex>& lc, vector<string>& vs, string s)
{
int i1 = count(lc.begin() ,lc.end() ,complex(1,3)) ;
int i2 = count(vs.begin() ,vs.end() ,"Diogenes") ;
int i3 = count(s.begin() ,s.end() ,'x') ;
}```

The use of a sequence allows us to use count for a built-in array and also to count parts of a container. For example:

```void g(char cs[] , int sz)
{
int i1 = count(&cs ,&cs[sz] ,'z') ; // 'z's in array
int i2 = count(&cs ,&cs[sz/2] ,'z') ; // 'z's in first half of array
}```

### 3.8.2 Iterator Types

What are iterators really? Any particular iterator is an object of some type. There are, however, many different iterator types because an iterator needs to hold the information necessary for doing its job for a particular container type. These iterator types can be as different as the containers and the specialized needs they serve. For example, a vector's iterator is most likely an ordinary pointer because a pointer is quite a reasonable way of referring to an element of a vector:

Figure 2

Alternatively, a vector iterator could be implemented as a pointer to the vector plus an index:

Figure 3

Using such an iterator would allow range checking.

A list iterator must be something more complicated than a simple pointer to an element because an element of a list in general does not know where the next element of that list is. Thus, a list iterator might be a pointer to a link:

Figure 4

What is common for all iterators is their semantics and the naming of their operations. For example, applying ++ to any iterator yields an iterator that refers to the next element. Similarly, * yields the element to which the iterator refers. In fact, any object that obeys a few simple rules like these is an iterator. Furthermore, users rarely need to know the type of a specific iterator; each container ''knows'' its iterator types and makes them available under the conventional names iterator and const_iterator. For example, list<Entry>: :iterator is the general iterator type for list<Entry>. I rarely have to worry about the details of how that type is defined.

### 3.8.3 Iterators and I/O

Iterators are a general and useful concept for dealing with sequences of elements in containers. However, containers are not the only place where we find sequences of elements. For example, an input stream produces a sequence of values and we write a sequence of values to an output stream. Consequently, the notion of iterators can be usefully applied to input and output.

To make an ostream_iterator, we need to specify which stream will be used and the type of objects written to it. For example, we can define an iterator that refers to the standard output stream, cout:

`ostream_iterator<string> oo(cout) ;`

The effect of assigning to *oo is to write the assigned value to cout. For example:

```int main()
{
*oo = "Hello, ";   // meaning cout ___"Hello, "
++oo;
*oo = "world!\n";  // meaning cout ___"world!\n"
}```

This is yet another way of writing the canonical message to standard output. The ++oo is done to mimic writing into an array through a pointer. This way wouldn't be my first choice for that simple task, but the utility of treating output as a write-only container will soon be obvious—if it isn't already.

Similarly, an istream_iterator is something that allows us to treat an input stream as a read-only container. Again, we must specify the stream to be used and the type of values expected:

`istream_iterator<string> ii(cin) ;`

Because input iterators invariably appear in pairs representing a sequence, we must provide an istream_iterator to indicate the end of input. This is the default istream_iterator:

`istream_iterator<string> eos;`

We could now read Hello, world! from input and write it out again like this:

```int main()
{
string s1 = *ii;
++ii;
string s2 = *ii;

cout << s1 << ' ' << s2 << '\n';
}```

Actually, istream_iterators and ostream_iterators are not meant to be used directly. Instead, they are typically provided as arguments to algorithms. For example, we can write a simple program to read a file, sort the words read, eliminate duplicates, and write the result to another file:

```int main()
{
string from, to;
cin >> from >> to;           // get source and target file names

ifstream is(from.c_str()) ;      // input stream (c_str(); see §3.5.1)
istream_iterator<string> ii(is) ;   // input iterator for stream
istream_iterator<string> eos;     // input sentinel

vector<string> b(ii,eos) ;       // b is a vector initialized from input
sort(b.begin() ,b.end()) ;       // sort the buffer

ofstream os(to.c_str()) ;       // output stream
ostream_iterator<string> oo(os,"\n") ; // output iterator for stream

unique_copy(b.begin() ,b.end() ,oo) ; // copy buffer to output,

return !is.eof() || !os;        // return error state (§3.2)
}```

An ifstream is an istream that can be attached to a file, and an ofstream is an ostream that can be attached to a file. The ostream_iterator's second argument is used to delimit output values.

### 3.8.4 Traversals and Predicates

Iterators allow us to write loops to iterate through a sequence. However, writing loops can be tedious, so the standard library provides ways for a function to be called for each element of a sequence.

Consider writing a program that reads words from input and records the frequency of their occurrence. The obvious representation of the strings and their associated frequencies is a map:

`map<string,int> histogram;`

The obvious action to be taken for each string to record its frequency is as follows:

```void record(const string& s)
{
histogram[s]++; // record frequency of ''s''
}```

Once the input has been read, we would like to output the data we have gathered. The map consists of a sequence of (string,int) pairs. Consequently, we would like to call

```void print(const pair<const string,int>& r)
{
cout << r.first << ' ' << r.second << '\n';
}```

for each element in the map (the first element of a pair is called first, and the second element is called second). The first element of the pair is a const string rather than a plain string because all map keys are constants.

Thus, the main program becomes:

```int main()
{
istream_iterator<string> ii(cin) ;
istream_iterator<string> eos;

for_each(ii,eos,record) ;
for_each(histogram.begin() ,histogram.end() ,print) ;
}```

Note that we don't need to sort the map to get the output in order. A map keeps its elements ordered so that an iteration traverses the map in (increasing) order.

Many programming tasks involve looking for something in a container rather than simply doing something to every element. For example, the find algorithm provides a convenient way of looking for a specific value. A more general variant of this idea looks for an element that fulfills a specific requirement. For example, we might want to search a map for the first value larger than 42. A map is a sequence of (key,value) pairs, so we search that list for a pair<const string,int> where the int is greater than 42:

```bool gt_42(const pair<const string,int>& r)
{
return r.second>42;
}

void f(map<string,int>& m)
{
typedef map<string,int>: :const_iterator MI;
MI i = find_if(m.begin() ,m.end() ,gt_42) ;
// ...
}```

Alternatively, we could count the number of words with a frequency higher than 42:

```void g(const map<string,int>& m)
{
int c42 = count_if(m.begin() ,m.end() ,gt_42) ;
// ...
}```

A function, such as gt_42(), that is used to control the algorithm is called a predicate. A predicate is called for each element and returns a Boolean value, which the algorithm uses to perform its intended action. For example, find_if() searches until its predicate returns true to indicate that an element of interest has been found. Similarly, count_if() counts the number of times its predicate is true.

The standard library provides a few useful predicates and some templates that are useful for creating more.

### 3.8.5 Algorithms Using Member Functions

Many algorithms apply a function to elements of a sequence. For example, in §3.8.4,

`for_each(ii,eos,record) ;`

calls record() for each string read from input.

Often, we deal with containers of pointers and we really would like to call a member function of the object pointed to, rather than a global function on the pointer. For example, we might want to call the member function Shape: :draw() for each element of a list<Shape*>. To handle this specific example, we simply write a nonmember function that invokes the member function. For example:

```void draw(Shape* p)
{
p->draw() ;
}

void f(list<Shape*>& sh)
{
for_each(sh.begin() ,sh.end() ,draw) ;
}```

By generalizing this technique, we can write the example like this:

```void g(list<Shape*>& sh)
{
for_each(sh.begin() ,sh.end() ,mem_fun(&Shape: :draw)) ;
}```

The standard library mem_fun() template takes a pointer to a member function as its argument and produces something that can be called for a pointer to the member's class. The result of mem_fun(&Shape: :draw) takes a Shape* argument and returns whatever Shape: :draw() returns.

The mem_fun() mechanism is important because it allows the standard algorithms to be used for containers of polymorphic objects.

### 3.8.6 Standard Library Algorithms

What is an algorithm? Knuth's general definition of an algorithm is ''a finite set of rules which gives a sequence of operations for solving a specific set of problems [and] has five important features: Finiteness...Definiteness...Input...Output...Effectiveness." In the context of the C++ standard library, an algorithm is a set of templates operating on sequences of elements.

The standard library provides dozens of algorithms. The algorithms are defined in namespace std and presented in the <algorithm> header. Here are a few I have found particularly useful:

#### Selected Standard Algorithms

 for_each() Invoke function for each element find() Find first occurrence of arguments find_if() Find first match of predicate count() Count occurrences of element count_if() Count matches of predicate replace() Replace element with new value replace_if() Replace element that matches predicate with new value copy() Copy elements unique_copy() Copy elements that are not duplicates sort() Sort elements equal_range() Find all elements with equivalent values merge() Merge sorted sequences

These algorithms and many more can be applied to elements of containers, strings, and built-in arrays.