templateForwardIterator min_elementForwardIterator first, ForwardIterator last; template ForwardIterator min_elementForwardIterator first, ForwardIterator last, Compare comp; The min_ele
Trang 1template<class ForwardIterator>
ForwardIterator min_element(ForwardIterator first, ForwardIterator last);
template<class ForwardIterator, class Compare>
ForwardIterator min_element(ForwardIterator first, ForwardIterator last,
Compare comp);
The min_element() function returns the first iterator it in the range [first, last) such
that no element in the range is less than *it The first version uses < to determine the
ordering, while the second version uses the comp comparison object
template<class ForwardIterator>
ForwardIterator max_element(ForwardIterator first, ForwardIterator last);
template<class ForwardIterator, class Compare>
ForwardIterator max_element(ForwardIterator first, ForwardIterator last,
Compare comp);
The max_element() function returns the first iterator it in the range [first, last) such
that there is no element that *it is less than The first version uses < to determine the
ordering, while the second version uses the comp comparison object
template<class InputIterator1, class InputIterator2>
bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2);
template<class InputIterator1, class InputIterator2, class Compare>
bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
Compare comp);
The lexicographical_compare() function returns true if the sequence of elements in
range [first1, last1) is lexicographically less than the sequence of elements in range
[first2, last2) and false otherwise A lexicographical comparison compares the first
element of one sequence to the first of the second, that is, it compares *first1 to
*first2 If *first1 is less than *first2, the function returns true If *first2 is less than
*first1, the function returns false If the two are equivalent, comparison proceeds to
This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it Thanks
Trang 2the next element in each sequence This process continues until two corresponding
elements are not equivalent or until the end of a sequence is reached If two
sequences are equivalent until the end of one is reached, the shorter sequence is
less If the two sequences are equivalent and of the same length, neither is less, so
the function returns false The first version of the function uses < to compare elements
and the second version uses the comp comparison object The lexicographic
comparison is a generalization of an alphabetic comparison
Permutations
A permutation of a sequence is a reordering of the elements For example, a
sequence of three elements has six possible orderings, because you have a choice of
three elements for the first element Choosing a particular element for the first position
leaves a choice of two for the second, and one for the third For example, the six
permutations of the digits 1, 3, and 5 are as follows:
123 132 213 232 312 321
In general, a sequence of n elements has n*(n-1)*…*1, or n! possible permutations
The permutation functions assume that the set of all possible permutations can be
arranged in lexicographical order, as in the previous example of six permutations That
means, in general, that there is a specific permutation that precedes and follows each
permutation For example, 213 immediately precedes 232, and 312 immediately
follows it However, the first permutation (123 in the example) has no predecessor,
and the last permutation (321) has no follower
template<class BidirectionalIterator>
bool next_permutation(BidirectionalIterator first,
BidirectionalIterator last);
template<class BidirectionalIterator, class Compare>
bool next_permutation(BidirectionalIterator first,
BidirectionalIterator last, Compare comp);
The next_permutation() function transforms the sequence in range [first, last) to the
next permutation in lexicographic order If the next permutation exists, the function
Trang 3returns true If it doesn't exist (that is, the range contains the last permutation in
lexicographic order), the function returns false and transforms the range to the first
permutation in lexicographic order The first version uses < to determine the ordering,
while the second version uses the comp comparison object
template<class BidirectionalIterator>
bool prev_permutation(BidirectionalIterator first,
BidirectionalIterator last);
template<class BidirectionalIterator, class Compare>
bool prev_permutation(BidirectionalIterator first,
BidirectionalIterator last, Compare comp);
The previous_permutation() function transforms the sequence in range [first, last) to
the previous permutation in lexicographic order If the previous permutation exists, the
function returns true If it doesn't exist (that is, the range contains the first permutation
in lexicographic order), the function returns false and transforms the range to the last
permutation in lexicographic order The first version uses < to determine the ordering,
while the second version uses the comp comparison object
Numeric Operations
Table G.12 summarizes the numeric operations, which are described by the numeric
header file Arguments are not shown, and overloaded functions are listed just once
Each function has a version that uses < for ordering elements and a version that uses
a comparison function object for ordering elements A fuller description including the
prototypes follows the table Thus, you can scan the table to get an idea of what a
function does; then look up the details if you find the function appealing
Table G.12 Sorting and Related Operations
accumulate() Calculates a cumulative total for values in a range
inner_product() Calculates the inner product of two ranges
partial_sum() Copies partial sums calculated from one range into a second
range
This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it Thanks
Trang 4adjacent_difference()Copies adjacent differences calculated from elements in one
range to a second range
template <class InputIterator, class T>
T accumulate(InputIterator first, InputIterator last, T init);
template <class InputIterator, class T, class BinaryOperation>
T accumulate(InputIterator first, InputIterator last, T init,
BinaryOperation binary_op);
The accumulate() function initializes a value acc to init; then it performs the operation
acc = acc + *i (first version) or acc = binary_op(acc, *i) (second version) for each
iterator i in the range [first, last) in order It then returns the resulting value of acc
template <class InputIterator1, class InputIterator2, class T>
T inner_product(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, T init);
template <class InputIterator1, class InputIterator2, class T,
class BinaryOperation1, class BinaryOperation2>
T inner_product(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, T init,
BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);
The inner_product() function initializes a value acc to init; then it performs the
operation acc = *i * *j (first version) or acc = binary_op(*i, *j) (second version) for
each iterator i in the range [first1, last1) in order and each corresponding iterator j in
the range [first2, first2 + (last1 - first1)) That is, it calculates a value from the first
elements from each sequence, then from the second elements of each sequence, and
so on, until it reaches the end of the first sequence (Hence the second sequence
should be at least as long as the first.) The function then returns the resulting value of
acc
template <class InputIterator, class OutputIterator>
OutputIterator partial_sum(InputIterator first, InputIterator last,
OutputIterator result);
Trang 5template <class InputIterator, class OutputIterator, class BinaryOperation>
OutputIterator partial_sum(InputIterator first, InputIterator last,
OutputIterator result,
BinaryOperation binary_op);
The partial_sum() function assigns *first to *result, *first + *(first + 1) to *(result + 1),
(first version) or binary_op(*first, *(first + 1)) to *(result + 1) (second version, and so
on That is, the nth element of the sequence beginning at result contains the sum (or
binary_op equivalent) of the first n elements of the sequence beginning at first The
function returns the past-the-end iterator for the result The algorithm allows result to
be first, that is, it allows the result to be copied over the original sequence, if desired
template <class InputIterator, class OutputIterator>
OutputIterator adjacent_difference(InputIterator first, InputIterator last,
OutputIterator result);
template <class InputIterator, class OutputIterator, class BinaryOperation>
OutputIterator adjacent_difference(InputIterator first, InputIterator last,
OutputIterator result,
BinaryOperation binary_op);
The adjacent_difference() function assigns *first to the location result (*result =
*first) Subsequent locations in the target range are assigned the differences (or
binary_op equivalent) of adjacent locations in the source range That is, the next
location in the target range (result + 1) is assigned *(first + 1) - *first (first version) or
binary_op(*(first + 1), *first) (second version, and so on The function returns the
past-the-end iterator for the result The algorithm allows result to be first, that is, it
allows the result to be copied over the original sequence, if desired
CONTENTS
This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it Thanks
Trang 6Appendix H SELECTED READINGS
Bibliography
Booch, Grady Object-Oriented Analysis and Design Reading, MA:
Addison-Wesley, 1993 This book presents the concepts behind OOP, discusses OOP
methods, and presents sample applications The examples are in C++
Booch, Grady, Jim Rumbaugh, and Ivar Jacobson Unified Modeling Language User
Guide Reading, MA: Addison-Wesley, 1998 This book by the creators of the Unified
Modeling Language presents the core of UML along with many examples of its use
Cline, Marshall, Greg Lomow, and Mike Girou C++ FAQs, Second Edition Reading,
MA: Addison-Wesley, 1999 This book addresses a great number of frequently asked
questions about the C++ language
Jacobson, Ivar Object-Oriented Software Engineering: A Use Case Driven
Approach Reading, MA: Addison-Wesley, 1994 This book describes successful
guidelines and methods (Object-Oriented Software Engineering, or OOSE) for
developing large-scale software systems
Josuttis, Nicolai M The C++ Standard Library: A Tutorial and Reference Reading,
MA: Addison-Wesley, 1999 This book describes the STL as well as other C++ library
features, such as complex number support, locales, and input/output streams
Lee, Richard C and William M Tepfenhart UML and C++, Second Edition Upper
Saddle River, New Jersey: Prentice Hall, 2001 This book is a self-teaching guide to
the Unified Modeling Language, and it includes a review of C++ fundamentals
Meyers, Scott Effective C++: 50 Specific Ways to Improve Your Programs and
Designs, Second Edition Reading, MA: Addison-Wesley, 1998 This book is aimed at
programmers who already know C++, and it provides 50 rules and guidelines Some
are technical, such as explaining when you should define copy constructors and
Trang 7assignment operators Others are more general, such as discussing is-a and has-a
relationships
Meyers, Scott Effective STL: 50 Specific Ways to Improve Your Use of the
Standard Template Library Reading, MA: Addison-Wesley, 2001 This book
provides guidance in choosing containers and algorithms and in other facets of using
the STL
Meyers, Scott More Effective C++: 35 New Ways to Improve Your Programs and
Designs Reading, MA: Addison-Wesley, 1996 This book continues in the tradition of
Effective C++, clarifying some of the more obscure aspects of the language and
showing how to accomplish various goals, such as designing smart pointers It reflects
the additional experience C++ programmers have gained the last few years
Rumbaugh, James, Michael Blaha, William Premerlani, Frederick Eddy, Bill Lorensen,
William Lorenson Object-Oriented Modeling and Design Englewood Cliffs, Prentice
Hall, 1991 This book presents and explores the Object Modeling Technique (OMT), a
method for breaking problems into suitable objects
Rumbaugh, James, Ivar Jacobson, and Grady Booch Unified Modeling Reference
Manual Reading, MA: Addison-Wesley, 1998 This book by the creators of the
Unified Modeling Language presents the complete description, in reference manual
format, of the UML
Stroustrup, Bjarne The C++ Programming Language. Third Edition Reading, MA:
Addison-Wesley, 1997 Stroustrup created C++, so this is the definitive text However,
it is most easily digested if you already have some knowledge of C++ It not only
describes the language, it also provides many examples of how to use it, as well as
discussions of OOP methodology Successive editions of this book have grown with
the language, and this edition includes a discussion of standard library elements such
as the STL and strings
Stroustrup, Bjarne The Design and Evolution of C++ Reading, MA:
Addison-Wesley, 1994
If you're interested in learning how C++ evolved and why it is the way it is, read this
book
This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it Thanks
Trang 8The ISO/ANSI standard (ISO/IEC 14882:1998):
A printed version of the standard is available for $231 from
Global Engineering Documents, Inc
15 Inverness Way East
Englewood, CO 80122-5704
You can order from its Web site:
http://global.ihs.com
A downloadable electronic version (pdf format) is available for $18 from the American
National Standards Institute Web site:
http://www.ansi.org
Internet Resources:
The C++ FAQ Lite site for frequently asked questions (in English, Chinese, French,
Russian, and Portuguese) is a slimmed down version of the book by Cline, et al
Currently it has the following URL:
http://www.parashift.com/c++-faq-lite/
You can find a moderated discussion of C++ questions in the following newsgroup:
comp.lang.c++.moderated
CONTENTS
Trang 9Appendix I CONVERTING TO ANSI/ISO
STANDARD C++
Preprocessor Directives Use Function Prototypes Type Casts
Become Familiar with C++ Features Use the New Header Organization Use Namespaces
Use the autoptr Template Use the string Class Use the STL
You might have programs (or programming habits) developed in C or in older versions
of C++ that you want to convert to Standard C++ This appendix provides some
guidelines Some pertain to moving from C to C++, others from older C++ to Standard
C++
Preprocessor Directives
The C/C++ preprocessor provides an array of directives In general, C++ practice is to
use those directives designed to manage the compilation process and to avoid using
directives as a substitute for code For example, the #include directive is an essential
component for managing program files Other directives, such as #ifndef and #endif,
let you control whether particular blocks of code get compiled The #pragma directive
lets you control compiler-specific compilation options These all are useful, sometimes
necessary, tools You should exert caution, however, when it comes to the #define
directive
Use const Instead of #define to Define Constants
This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it Thanks .
Trang 10Symbolic constants make code more readable and maintainable The constant's name
indicates its meaning, and if you need to change the value, you just have to change
the value once, in the definition, then recompile C used the preprocessor for this
purpose:
#define MAX_LENGTH 100
The preprocessor then does a text substitution in your source code, replacing
occurrences of MAX_LENGTH with 100 prior to compilation
The C++ approach is to apply the const modifier to a variable declaration:
const int MAX_LENGTH = 100;
This treats MAX_LENGTH as a read-only int
There are several advantages to the const approach First, the declaration explicitly
names the type For #define, you must use various suffixes to a number to indicate
types other than char, int, or double; for example, using 100L to indicate a long type
or 3.14F to indicate a float type More importantly, the const approach can just as
easily be used with derived types:
const int base_vals[5] = {1000, 2000, 3500, 6000, 10000};
const string ans[3] = {"yes", "no", "maybe"};
Finally, const identifiers obey the same scope rules as variables Thus, you can create
constants with global scope, named namespace scope, and block scope If, say, you
define a constant in a particular function, you don't have to worry about the definition
conflicting with a global constant used elsewhere in a program For example, consider
the following:
#define n 5
const int dz = 12;
void fizzle()
{
int n;
int dz;