_STL Algorithms_ by Dan Zigmond Listing One template InputIterator find(InputIterator first, InputIterator last, const T& value); (b) template InputIterator find(InputIterator first, InputIterator last, Predicate pred); (c) template InputIterator find_if(InputIterator first, InputIterator last, Predicate pred){ while (first != last && !pred(*first)) ++first; return first; } (d) const char* the_line = "This is a line of text."; char* the_space = find_if(the_line, the_line + strlen(the_line), isspace); Listing Three (a) template void count(InputIterator first, InputIterator last, const T& value, Size& n); template void count_if(InputIterator first,InputIterator last,Predicate pred,Size& n); (b) int n = 0; count_if(the_line, the_line + strlen(the_line), isspace, n); (c) count(the_line, the_line + strlen(the_line), ' ', n); Listing Four (a) template bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2); template bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred); (b) template Function for_each(InputIterator first, InputIterator last, Function f); (c) int numbers[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; for_each(numbers, numbers+10, sin); (d) static unsigned counter = 0; void print_and_count(int i) { cout << "The number is: " << i << endl; ++counter; } void main() { int numbers[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; for_each(numbers, numbers+10, print_and_count); cout << "The total number of numbers printed is: " << counter; } Listing Five (a) template OutputIterator copy(InputIterator first, InutIterator last, OutputIterator result); (b) template OutputIterator transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op); Listing Six (a) int numbers[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; transform(numbers, numbers+10, numbers, sin); (b) template void replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value); template ForwardIterator remove(ForwardIterator first, ForwardIterator last, const T& value); (c) int numbers[5] = {1, 3, 5, 7, 9}; int* end = remove(numbers, numbers+5, 5); Listing Seven template void reverse(BidirectionalIterator first, BidirectionalIterator last) template void reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result) Listing Eight (a) template void fill(ForwardIterator first, ForwardIterator last, const T& value); template void fill_n(OutputIterator first, Size n, const T& value); (b) template void fill(ForwardIterator first, ForwardIterator last, const T& value) { while (first != last) *first++ = value; } template void fill_n(OutputIterator first, Size n, const T& value) { while (n-- > 0) *first++ = value; } Listing Nine template void generate(ForwardIterator first, ForwardIterator last, Generator gen); template void generate(OutputIterator first, Size n, Generator gen); Listing Ten (a) void main() { vector< int > numbers; copy(istream_iterator< int >(cin), istream_iterator< int >(), inserter(numbers, numbers.end())); sort(numbers.begin(), numbers.end()); copy(numbers.begin(), numbers.end(), ostream_iterator< int >(cout, ' ')); } (b) template void sort(RandomAccessIterator first, RandomAccessIterator last); template void sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp); (c) template void partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last); template void partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp); (d) template void partial_sort_copy(InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last); template void partial_sort_copy(InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp); Listing Eleven (a) template void nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last); template void nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp); (b) template const T& min(const T& a, const T& b); template const T& max(const T& a, const T& b); (c) template InputIterator min_element(InputIterator first, InputIterator last); template InputIterator min_element(InputIterator first,InputIterator last,Compare comp); template InputIterator max_element(InputIterator first, InputIterator last); template InputIterator max_element(InputIterator first,InputIterator last,Compare comp);