Templates 711
19
19:
20: fill(vInt.begin(), vInt.begin() + 5, 1);
21: fill(vInt.begin() + 5, vInt.end(), 2);
22:
23: for_each(vInt.begin(), vInt.end(), DoPrint);
24: cout << endl << endl;
25:
26: return 0;
27: }
1 1 1 1 1 2 2 2 2 2
The only new content in this listing is on lines 20 and 21, where the fill()algo-
rithm is used. The fill algorithm fills the elements in a sequence with a given
value. On line 20, it assigns an integer value 1 to the first five elements in vInt. The last
five elements of vIntare assigned with integer 2 on line 21.
Sorting and Related Operations
The third category of algorithms is the sorting and related operations subclass. Within
this set of operations, you find merging, partial sorts, partial sorts with copying, binary
searches, lower and upper bounds checks, set intersections, set differencing, minimums,
maximums, permutations, and more. You can check your compiler’s documentation or
the C++ standards documentation for specific information on each of these operations.
OUTPUT
LISTING19.13 continued
ANALYSIS
It is beyond the scope of this book to go into details on all the operations in
the algorithm and other Standard Template Library classes. You can check
your compiler’s documentation or the C++ standards to get more details on
the classes and operations available as well as the details on their parame-
ters and usage. In addition, entire books are available on the STL and its
usage.
NOTE
Summary ..............................................................................................................
Today, you learned how to create and use templates. Templates are a key part of the C++
standard and a built-in facility of C++. Templates are used to create parameterized
types—types that change their behavior based on parameters passed in at creation. They
are a way to reuse code safely and effectively.