CHAPTER 17STL Algorithms in Detail

17.1 Introduction and Objectives

The goal of this chapter is to discuss how STL supports a range of algorithms that operate on STL containers. In general, an algorithm is a complete, step-by-step procedure that solves a given problem. Each step must be expressed in terms of a finite number of rules and each step is guaranteed to terminate in a finite number of application of the rules. In most cases a rule corresponds to the execution of one or more operations. A sequential algorithm performs operations one at a time in sequence. This is in contrast to distributed and parallel algorithms that can perform many operations simultaneously (Berman and Paul, 2005). In this chapter we discuss how STL supports algorithms that operate on sequence and associative containers. It is important to state that the design of the STL algorithms is based on the functional programming paradigm. Instead of encapsulating data and operations in a class or struct (as we are accustomed to do with the object-oriented programming paradigm), STL separates data from algorithms by iterators playing the role of mediator between them. In particular, a common scenario is to iterate in a generic container from beginning to end and then apply an operation to each element in that container. The code in Chapters 17 and 18 is machine readable and it can be used as exemplar code for your own applications.

In order to motivate this paradigm we introduce the for_each() algorithm ...

Get Financial Instrument Pricing Using C++, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.