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 must have compatible types.

None of these operations depends on the type of the container that holds the elements. So long as there is an iterator that can be used to access the elements, find doesn’t depend in any way on the container type (or even whether the elements are stored in a container).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.176.194