How the Algorithms Work

To see how the algorithms can be used on varying types of containers, let’s look a bit more closely at find. Its job is to find a particular element in an unsorted sequence of elements. Conceptually, we can list the steps find must take:

1. It accesses the first element in the sequence.

2. It compares that element to the value we want.

3. If this element matches the one we want, find returns a value that identifies this element.

4. Otherwise, find advances to the next element and repeats steps 2 and 3.

5. find must stop when it has reached the end of the sequence.

6. If find gets to the end of the sequence, it needs to return a value indicating that the element was not found. This value and the one returned from step 3 ...

Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.