const LessThanComparable& min(const LessThanComparable& a, const LessThanComparable& b);const T& min(const T& a, const T& b,...

Pokaż mi serce nie opętane zwodniczymi marzeniami, a pokażę ci człowieka szczęśliwego.

The first version performs comparisons using operator< and the second passes both arguments to binary_pred to perform the comparison.Comment
const LessThanComparable& max(const LessThanComparable& a, const LessThanComparable& b);const T& max(const T& a, const T& b, BinaryPredicate binary_pred);
Exactly like min( ), but returns the greater of its two arguments.Comment
void swap(Assignable& a, Assignable& b);void iter_swap(ForwardIterator1 a, ForwardIterator2 b);Comment
Exchanges the values of a and b using assignment. Note that all container classes use specialized versions of swap( ) that are typically more efficient than this general version.Comment
iter_swap( ) is a backwards-compatible remnant in the standard; you can just use swap( ).Comment
Creating your own STL-style algorithms
Once you become comfortable with the STL algorithm style, you can begin to create your own STL-style algorithms. Because these will conform to the format of all the other algorithms in the STL, they’re easy to use for programmers who are familiar with the STL, and thus become a way to “extend the STL vocabulary.”Comment
The easiest way to approach the problem is to go to the <algorithm> header file and find something similar to what you need, and modify that (virtually all STL implementations provide the code for the templates directly in the header files). For example, an algorithm that stands out by its absence is copy_if( ) (the closest approximation is partition( )), which was used in Binder1.cpp at the beginning of this chapter, and in several other examples in this chapter. This will only copy an element if it satisfies a predicate. Here’s an implementation:Comment
//: C08:copy_if.h
// Roll your own STL-style algorithm
#ifndef COPY_IF_H
#define COPY_IF_H
 
template<typename ForwardIter,
typename OutputIter, typename UnaryPred>
OutputIter copy_if(ForwardIter begin, ForwardIter end,
OutputIter dest, UnaryPred f) {
while(begin != end) {
if(f(*begin))
*dest++ = *begin;
begin++;
}
return dest;
}
#endif // COPY_IF_H ///:~
 
The return value is the past-the-end iterator for the destination sequence (the copied sequence).Comment
 
Now that you’re comfortable with the ideas of the various iterator types, the actual implementation is quite straightforward. You can imagine creating an entire additional library of your own useful algorithms that follow the format of the STL.Comment
Summary
The goal of this chapter, and the previous one, was to give you a programmer’s-depth understanding of the containers and algorithms in the Standard Template Library. That is, to make you aware of and comfortable enough with the STL that you begin to use it on a regular basis (or at least, to think of using it so you can come back here and hunt for the appropriate solution). It is powerful not only because it’s a reasonably complete library of tools, but also because it provides a vocabulary for thinking about problem solutions, and because it is a framework for creating additional tools.Comment
Although this chapter and the last did show some examples of creating your own tools, I did not go into the full depth of the theory of the STL that is necessary to completely understand all the STL nooks and crannies to allow you to create tools more sophisticated than those shown here. I did not do this partially because of space limitations, but mostly because it is beyond the charter of this book; my goal here is to give you practical understanding that will affect your day-to-day programming skills.Comment
There are a number of books dedicated solely to the STL (these are listed in the appendices), but the two that I learned the most from, in terms of the theory necessary for tool creation, were first, Generic Programming and the STL by Matthew H. Austern, Addison-Wesley 1999 (this also covers all the SGI extensions, which Austern was instrumental in creating), and second (older and somewhat out of date, but still quite valuable), C++ Programmer’s Guide to the Standard Template Library by Mark Nelson, IDG press 1995.Comment
Exercises
Copyright (c) 2009 Pokaż mi serce nie opętane zwodniczymi marzeniami, a pokażę ci człowieka szczęśliwego. | Powered by Wordpress. Fresh News Theme by WooThemes - Premium Wordpress Themes.