Tải bản đầy đủ (.pdf) (20 trang)

C++ Primer Plus (P70) ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (48.4 KB, 20 trang )

remove_copy_if()Copies elements from one range to another, omitting elements for
which a predicate function object returns true.
unique()Reduces each sequence of two or more equivalent elements in a
range to a single element.
unique_copy()Copies elements from one range to another, reducing each
sequence of two or more equivalent elements to one.
reverse()Reverses the elements in a range.
reverse_copy()Copies a range in reverse order to a second range.
rotate()Treats a range as a circular ordering and rotates the elements left.
rotate_copy()Copies one range to another in a rotated order.
random_shuffle()Randomly rearranges the elements in a range.
partition()Places all the elements that satisfy a predicate function object
before all elements that don't.
stable_partition()Places all the elements that satisfy a predicate function object
before all elements that don't. The relative order of elements in
each group is preserved.
Now let's go to the prototypes. As you saw earlier, pairs of iterators indicate ranges,
with the chosen template parameter name indicating the type of iterator. As usual a
range of the form [first, last) goes from first up to, but not including last. Functions
passed as arguments are function objects, which can be function pointers or objects
for which the () operation is defined. As in Chapter 16, a predicate is a Boolean
function with one argument, and a binary predicate is a Boolean function with two
arguments. (The functions need not be type bool as long as they return a 0 value for
false and a non-zero for true.) Also, as in Chapter 15, a unary function object is one
taking a single argument, and a binary function object is one taking two arguments.
template<class InputIterator, class OutputIterator>
OutputIterator copy(InputIterator first, InputIterator last,
OutputIterator result);
The copy() function copies the elements in the range [first, last) into the range [result,
result + (last - first)). It returns result + (last - first), that is, an iterator pointing one
past the last copied-to location. The function requires that result not be in the range


[first, last), that is, the target can't overlap the source.
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
template<class BidirectionalIterator1, class BidirectionalIterator2>
BidirectionalIterator2 copy_backward(BidirectionalIterator1 first,
BidirectionalIterator1 last, BidirectionalIterator2 result);
The copy_backward() function copies the elements in the range [first, last) into the
range [result -(last - first), result). Copying begins with the element at last -1 being
copied to location result - 1, and proceeds backwards from there to first. It returns
result - (last - first), that is, an iterator pointing one past the last copied-to location.
The function requires that result not be in the range [first, last). However, because
copying is done backwards, it is possible for the target and source to overlap.
template<class T> void swap(T& a, T& b);
The swap() function exchanges values stored at two locations specified by references.
template<class ForwardIterator1, class ForwardIterator2>
ForwardIterator2 swap_ranges(
ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2);
The swap_ranges() function exchanges values in the range [first1, last1) with the
corresponding values in the range beginning at first2. The two ranges should not
overlap.
template<class ForwardIterator1, class ForwardIterator2>
void iter_swap(ForwardIterator1 a, ForwardIterator2 b);
The iter_swap() function exchanges values stored at two locations specified by
iterators.
template<class InputIterator, class OutputIterator, class UnaryOperation>
OutputIterator transform(InputIterator first, InputIterator last,
OutputIterator result, UnaryOperation op);
This version of transform() applies the unary function object op to each element in the
range [first, last) and assigns the return value to the corresponding element in the
range beginning at result. So *result is set to op(*first), and so on. It returns result +

This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
(last - first), that is, the past-the-end value for the target range.
template<class InputIterator1, class InputIterator2, class OutputIterator,
class BinaryOperation>
OutputIterator transform(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, OutputIterator result,
BinaryOperation binary_op);
This version of transform() applies the binary function object op to each element in
the range [first1, last1) and to each element in the range [first2, last2) and assigns
the return value to the corresponding element in the range beginning at result. So
*result is set to op(*first1, *first2), and so on. It returns result + (last - first), the
past-the-end value for the target range.
template<class ForwardIterator, class T>
void replace(ForwardIterator first, ForwardIterator last,
const T& old_value, const T& new_value);
The replace() function replaces each occurrence of the value old_value in the range
[first, last) with the value new_value.
template<class ForwardIterator, class Predicate, class T>
void replace_if(ForwardIterator first, ForwardIterator last,
Predicate pred, const T& new_value);
The replace()_if function replaces each value old in the range [first, last) for which
pred(old) is true with the value new_value.
template<class InputIterator, class OutputIterator, class T>
OutputIterator replace_copy(InputIterator first, InputIterator last,
OutputIterator result,const T& old_ value, const T& new_ value);
The replace_copy() function copies the elements in the range [first, last) to a range
beginning at result, but substituting new_value for each occurrence of old_value. It
returns result + (last - first), the past-the-end value for the target range.
template<class Iterator, class OutputIterator, class Predicate, class T>
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.

OutputIterator replace_copy_if(Iterator first, Iterator last,
OutputIterator result, Predicate pred, const T& new_ value);
The replace_copy_if() function copies the elements in the range [first, last) to a range
beginning at result, but substituting new_value for each value old for which pred(old)
is true. It returns result + (last - first), the past-the-end value for the target range.
template<class ForwardIterator, class T>
void fill(ForwardIterator first, ForwardIterator last, const T& value);
The fill() function sets each element in the range [first, last) to value.
template<class OutputIterator, class Size, class T>
void fill_n(OutputIterator first, Size n, const T& value);
The fill_n() function sets each of the first n elements beginning at location first to
value.
template<class ForwardIterator, class Generator>
void generate(ForwardIterator first, ForwardIterator last, Generator gen);
The generator() function sets each element in the range [first, last) to gen(), where
gen is a generator function object, that is, one that takes no arguments. For example,
gen can be a pointer to rand().
template<class OutputIterator, class Size, class Generator>
void generate_n(OutputIterator first, Size n, Generator gen);
The generator_n() function sets each of the first n elements in the range beginning at
first to gen(), where gen is a generator function object, that is, one that takes no
arguments. For example, gen can be a pointer to rand().
template<class ForwardIterator, class T>
ForwardIterator remove(ForwardIterator first, ForwardIterator last,
const T& value);
The remove() function removes all occurrences of value from the range [first, last)
and returns a past-the-end iterator for the resulting range. The function is stable,
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
meaning the order of the unremoved elements is unaltered.
Note

Because the various remove() and unique() functions
are not member functions, and also because they aren't
restricted to STL containers, they can't reset the size of
a container. Instead, they return an iterator indicating
the new past-the-end location. Typically, the removed
items simply are shifted to the end of the container.
However, for STL containers you can use the returned
iterator and one of the erase() methods to reset end().
template<class ForwardIterator, class Predicate>
ForwardIterator remove_if(ForwardIterator first, ForwardIterator last,
Predicate pred);
The remove_if() function removes all occurrences of values val for which pred(val) is
true from the range [first, last) and returns a past-the-end iterator for the resulting
range. The function is stable, meaning the order of the unremoved elements is
unaltered.
template<class InputIterator, class OutputIterator, class T>
OutputIterator remove_copy(InputIterator first, InputIterator last,
OutputIterator result, const T& value);
The remove_copy() function copies values from the range [first, last) to the range
beginning at result, skipping instances of value as it copies. It returns a past-the-end
iterator for the resulting range. The function is stable, meaning the order of the
unremoved elements is unaltered.
template<class InputIterator, class OutputIterator, class Predicate>
OutputIterator remove_copy_if(InputIterator first, InputIterator last,
OutputIterator result, Predicate pred);
The remove_copy_if() function copies values from the range [first, last) to the range
beginning at result, but skipping instances of val for which pred(val) is true as it
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
copies. It returns a past-the-end iterator for the resulting range. The function is stable,
meaning the order of the unremoved elements is unaltered.

template<class ForwardIterator>
ForwardIterator unique(ForwardIterator first, ForwardIterator last);
template<class ForwardIterator, class BinaryPredicate>
ForwardIterator unique(ForwardIterator first, ForwardIterator last,
BinaryPredicate pred);
The unique() function reduces each sequence of two or more equivalent elements in a
range [first, last) to a single element and returns a past-the-end iterator for the new
range. The first version uses the == operator for the value type to compare elements.
The second version uses the binary predicate function object pred to compare
elements. That is, elements pointed to by it1 and it2 match if pred(*it1, *it2) is true.
template<class InputIterator, class OutputIterator>
OutputIterator unique_copy(InputIterator first, InputIterator last,
OutputIterator result);
template<class InputIterator, class OutputIterator, class BinaryPredicate>
OutputIterator unique_copy(InputIterator first, InputIterator last,
OutputIterator result, BinaryPredicate pred);
The unique_copy() function copies elements from a range [first, last) to the range
beginning at result, reducing each sequence of two or more identical elements to a
single element. It returns a past-the-end iterator for the new range. The first version
uses the == operator for the value type to compare elements. The second version
uses the binary predicate function object pred to compare elements. That is, elements
pointed to by it1 and it2 match if pred(*it1, *it2) is true.
template<class BidirectionalIterator>
void reverse(BidirectionalIterator first, BidirectionalIterator last);
The reverse() function reverses the elements in the range [first, last) by invoking
swap(first, last - 1), and so on.
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
template<class BidirectionalIterator, class OutputIterator>
OutputIterator reverse_copy(BidirectionalIterator first,
BidirectionalIterator last,

OutputIterator result);
The reverse copy() function copies the elements in the range [first, last) to the range
beginning at result in reverse order. The two ranges should not overlap.
template<class ForwardIterator>
void rotate(ForwardIterator first, ForwardIterator middle,
ForwardIterator last);
The rotate() function performs a left-rotate on the elements in the range [first, last).
The element at middle is moved to first, the element at middle + 1 to first + 1, and so
on. The elements preceding middle are wrapped around to the end of the container so
that the element at first follows that formerly at last - 1.
template<class ForwardIterator, class OutputIterator>
OutputIterator rotate_copy(ForwardIterator first, ForwardIterator middle,
ForwardIterator last, OutputIterator result);
The rotate_copy() function copies the elements in the range [first, last) to the range
beginning at result using the rotated sequence described for rotate().
template<class RandomAccessIterator>
void random_shuffle(RandomAccessIterator first, RandomAccessIterator last);
This version of the random_shuffle() function shuffles the elements in the range [first,
last). The distribution is uniform; that is, each possible permutation of the original
order is equally likely.
template<class RandomAccessIterator, class RandomNumberGenerator>
void random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
RandomNumberGenerator& random);
This version of the random_shuffle() function shuffles the elements in the range [first,
last). The function object random determines the distribution. Given n elements, the
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
expression random(n) should return a value in the range [0,n).
template<class BidirectionalIterator, class Predicate>
BidirectionalIterator partition(BidirectionalIterator first,
BidirectionalIterator last,

Predicate pred);
The partition() function places each element whose value val is such that pred(val) is
true before all elements that don't meet that test. It returns an iterator to the position
following that last position holding a value for which the predicate object function was
true.
template<class BidirectionalIterator, class Predicate>
BidirectionalIterator stable_partition(BidirectionalIterator first,
BidirectionalIterator last,
Predicate pred);
The stable_partition() function places each element whose value val is such that
pred(val) is true before all elements that don't meet that test. The function preserves
the relative ordering within each of the two groups. It returns an iterator to the position
following that last position holding a value for which the predicate object function was
true.
Sorting and Related Operations
Table G.11 summarizes the sorting and related operations. 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.
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
Table G.11. Sorting and Related Operations
Function Description
sort()Sorts a range.
stable_sort()Sorts a range, preserving the relative order of
equivalent elements.
partial_sort()
Partially sorts a range, providing the first n elements of
a full sort.

partial_sort_copy()Copies a partially sorted range to another range.
nth_element()Given an iterator into a range, finds the element that
would be there if the range were sorted, and places that
element there.
lower_bound()Given a value, finds the first position in a sorted range
before which the value can be inserted while
maintaining the ordering.
upper_bound()Given a value, finds the last position in a sorted range
before which the value can be inserted while
maintaining the ordering.
equal_range()Given a value, finds the largest subrange of a sorted
range such that the value can be inserted before any
element in the subrange without violating the ordering.
binary_search()Returns true if a sorted range contains a value
equivalent to a given value, and false otherwise.
merge()Merges two sorted ranges into a third range.
inplace_merge()Merges two consecutive sorted ranges in place.
includes()Returns true if every element in one set also is found in
another set.
set_union()Constructs the union of two sets, which is a set
containing all elements present in either set.
set_intersection()Constructs the intersection of two sets, which is a set
containing only those elements found in both sets.
set_difference()Constructs the difference of two sets, which is a set
containing only those elements found in the first set but
not the second.
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
set_symmetric_difference()Constructs a set consisting of elements found in one set
or the other, but not both.
make_heapConverts a range to heap.

push_heap()Adds an element to a heap.
pop_heap()Removes the largest element from a heap.
sort_heap()Sorts a heap.
min()Returns the lesser of two values.
max()Returns the greater of two values.
min_element()Finds the first occurrence of the smallest value in a
range.
max_element()Finds the first occurrence of the largest value in a range.
lexicographic_compare()Compares two sequences lexicographically, returning
true if the first sequence is lexicographically less than
the second, and false otherwise.
next_permutation()Generates the next permutation in a sequence.
previous_permutation()Generates the preceding permutation in a sequence.
The functions in this section determine the order of two elements by using the <
operator defined for the elements or by using a comparison object designated by the
template type Compare. If comp is an object of type Compare, then comp(a,b) is a
generalization of a < b and returns true if a precedes b in the ordering scheme. If a < b
is false and b < a also is false, a and b are equivalent. A comparison object must
provide at least strict weak ordering. This means the following:
The expression comp(a,a) must be false, a generalization of the fact that a
value can't be less than itself. (This is the strict part.)
If comp(a,b) is true and comp(b,c) is true, then comp(a,c) is true (that is,
comparison is a transitive relationship).
If a is equivalent to b, and b is equivalent to c, then a is equivalent to c (that is,
equivalency is a transitive relationship).
If you think of applying < to integers, then equivalency implies equality, but this doesn't
have to hold for more general cases. For example, you could define a structure with
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
several members describing a mailing address and define a comp comparison object
that orders the structures according to ZIP code. Then any two addresses with the

same ZIP code would be equivalent but not equal.
Now let's go to the prototypes. We'll divide this section into several subsections. As
you saw earlier, pairs of iterators indicate ranges, with the chosen template parameter
name indicating the type of iterator. As usual a range of the form [first, last) goes from
first up to, but not including, last. Functions passed as arguments are function objects,
which can be pointers or objects for which the () operation is defined. As you learned
in Chapter 16, a predicate is a Boolean function with one argument, and a binary
predicate is a Boolean function with two arguments. (The functions need not be type
bool as long as they return a 0 value for false and a non-zero for true.) Also, as in
Chapter 16, a unary function object is one taking a single argument, and a binary
function object is one taking two arguments.
Sorting
First, let's examine the sorting algorithms.
template<class RandomAccessIterator>
void sort(RandomAccessIterator first, RandomAccessIterator last);
template<class RandomAccessIterator, class Compare>
void sort(RandomAccessIterator first, RandomAccessIterator last,
Compare comp);
The sort() function sorts the range [first, last) in increasing order, using the value type
< operator for comparison. The first version uses < and the second uses the
comparison object comp to determine the order.
template<class RandomAccessIterator>
void stable_sort(RandomAccessIterator first, RandomAccessIterator last);
template<class RandomAccessIterator, class Compare>
void stable_sort(RandomAccessIterator first, RandomAccessIterator last,
Compare comp);
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
The stable_sort() function sorts the range [first, last) preserving the relative order of
equivalent elements. The first version uses < and the second uses the comparison
object comp to determine the order.

template<class RandomAccessIterator>
void partial_sort(RandomAccessIterator first, RandomAccessIterator middle,
RandomAccessIterator last);
template<class RandomAccessIterator, class Compare>
void partial_sort(RandomAccessIterator first, RandomAccessIterator middle,
RandomAccessIterator last, Compare comp);
The partial_sort() function partially sorts the range [first, last). The first middle - first
elements of the sorted range are placed in the range [first, middle), and the remaining
elements are unsorted. The first version uses < and the second uses the comparison
object comp to determine the order.
template<class InputIterator, class RandomAccessIterator>
RandomAccessIterator partial_sort_copy(InputIterator first,
InputIterator last,
RandomAccessIterator result_first,
RandomAccessIterator result_last);
template<class InputIterator, class RandomAccessIterator, class Compare>
RandomAccessIterator
partial_sort_copy(InputIterator first, InputIterator last,
RandomAccessIterator result_first,
RandomAccessIterator result_last,
Compare comp);
The partial_sort_copy() function copies the first n elements of the sorted range [first,
last) to the range [result_first, result_first + n). The value of n is the lesser of last -
first and result_last - result_first. The function returns result_first + n. The first
version uses < and the second uses the comparison object comp to determine the
order.
template<class RandomAccessIterator>
void nth_element(RandomAccessIterator first, RandomAccessIterator nth,
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
RandomAccessIterator last);

template<class RandomAccessIterator, class Compare>
void nth_element(RandomAccessIterator first, RandomAccessIterator nth,
RandomAccessIterator last, Compare comp);
The nth_element() function finds the element in range [first, last) that would be at
position nth were the range sorted, and it places that element at position nth. The first
version uses < and the second uses the comparison object comp to determine the
order.
Binary Search
The algorithms in the binary search group assume the range is sorted. They only
require a forward iterator but are most efficient for random iterators.
template<class ForwardIterator, class T>
ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last,
const T& value);
template<class ForwardIterator, class T, class Compare>
ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last,
const T& value, Compare comp);
The lower_bound() function finds the first position in a sorted range [first, last) in front
of which value can be inserted without violating the order. It returns an iterator pointing
to this position. The first version uses < and the second uses the comparison object
comp to determine the order.
template<class ForwardIterator, class T>
ForwardIterator upper_bound(ForwardIterator first, ForwardIterator last,
const T& value);
template<class ForwardIterator, class T, class Compare>
ForwardIterator upper_bound(ForwardIterator first, ForwardIterator last,
const T& value, Compare comp);
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
The upper_bound() function finds the last position in a sorted range [first, last) in front
of which value can be inserted without violating the order. It returns an iterator pointing
to this position. The first version uses < and the second uses the comparison object

comp to determine the order.
template<class ForwardIterator, class T>
pair<ForwardIterator, ForwardIterator> equal_range(
ForwardIterator first, ForwardIterator last, const T& value);
template<class ForwardIterator, class T, class Compare>
pair<ForwardIterator, ForwardIterator> equal_range(
ForwardIterator first, ForwardIterator last, const T& value,
Compare comp);
The equal_range() function finds the largest subrange [it1, it2) in a sorted range [first,
last) such that value can be inserted in front of any iterator in this range without
violating the order. The function returns a pair formed of it1 and it2. The first version
uses < and the second uses the comparison object comp to determine the order.
template<class ForwardIterator, class T>
bool binary_search(ForwardIterator first, ForwardIterator last,
const T& value);
template<class ForwardIterator, class T, class Compare>
bool binary_search(ForwardIterator first, ForwardIterator last,
const T& value, Compare comp);
The binary_search() function returns true if the equivalent of value is found in the
sorted range [first, last) and false otherwise. The first version uses < and the second
uses the comparison object comp to determine the order.
Note
Recall that if < is used for ordering, values a and b are
equivalent if both a < b and b < a are false. For
ordinary numbers, equivalency implies equality, but this
is not the case for structures sorted on the basis of just
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
one member. Thus, there may be more than one
location where a new value can be inserted and still
keep the data ordered. Similarly, if the comparison

object comp is used for ordering, equivalency means
both comp(a,b) and comp(b,a) are false. (This is a
generalization of the statement that a and b are
equivalent if a is not less than b and b is not less than a.)
Merging
The merging functions assume ranges are sorted.
template<class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator merge(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result);
template<class InputIterator1, class InputIterator2, class OutputIterator,
class Compare>
OutputIterator merge(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Compare comp);
The merge() function merges elements from sorted range [first1, last1) and from
sorted range [first2, last2), placing the result in the range starting at result. The target
range should not overlap either of the merged ranges. When equivalent elements are
found in both ranges, elements from the first range precede elements of the second.
The return value is the past-the-end iterator for the resulting merge. The first version
uses < and the second uses the comparison object comp to determine the order.
template<class BidirectionalIterator>
void inplace_merge(BidirectionalIterator first,
BidirectionalIterator middle, BidirectionalIterator last);
template<class BidirectionalIterator, class Compare>
void inplace_merge(BidirectionalIterator first,
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
BidirectionalIterator middle, BidirectionalIterator last,
Compare comp);
The inplace_merge() function merges two consecutive sorted ranges—[first, middle)

and [middle, last)—into a single sorted sequence stored in the range [first, last).
Elements from the first range will precede equivalent elements from the second range.
The first version uses < and the second uses the comparison object comp to
determine the order.
Set Operations
Set operations work with all sorted sequences, including set and multiset. For
containers holding more than one instance of a value, such as multiset, definitions are
generalized. A union of two multisets contains the larger number of occurrences of
each element, and an intersection contains the lesser number of occurrences of each
element. For example, suppose multiset A contains the string "apple" seven times and
multiset B contains the same string four times. Then the union of A and B will contain
seven instances of "apple" and the intersection will contain four instances.
template<class InputIterator1, class InputIterator2>
bool includes(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2);
template<class InputIterator1, class InputIterator2, class Compare>
bool includes(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2, Compare comp);
The includes() function returns true if every element in range [first2, last2) also is
found in the range [first1, last1) and false otherwise. The first version uses < and the
second uses the comparison object comp to determine the order.
template<class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_union(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result);
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
template<class InputIterator1, class InputIterator2, class OutputIterator,
class Compare>
OutputIterator set_union(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,

OutputIterator result, Compare comp);
The set_union() function constructs the set that is the union of the ranges [first1,
last1) and [first2, last2) and copies the result to the location pointed to by result. The
resulting range should not overlap either of the original ranges. The function returns a
past-the-end iterator for the constructed range. The union is the set containing all
elements found in either or both sets. The first version uses < and the second uses
the comparison object comp to determine the order.
template<class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_intersection(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result);
template<class InputIterator1, class InputIterator2, class OutputIterator,
class Compare>
OutputIterator set_intersection(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Compare comp);
The set_intersection() function constructs the set that is the intersection of the ranges
[first1, last1) and [first2, last2) and copies the result to the location pointed to by
result. The resulting range should not overlap either of the original ranges. The
function returns a past-the-end iterator for the constructed range. The intersection is
the set containing those elements common to both sets. The first version uses < and
the second uses the comparison object comp to determine the order.
template<class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_difference(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result);
template<class InputIterator1, class InputIterator2, class OutputIterator,
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
class Compare>
OutputIterator set_difference(InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Compare comp);
The set_difference() function constructs the set that is the difference of the ranges
[first1, last1) and [first2, last2) and copies the result to the location pointed to by
result. The resulting range should not overlap either of the original ranges. The
function returns a past-the-end iterator for the constructed range. The difference is the
set containing those elements found in the first set but not in the second. The first
version uses < and the second uses the comparison object comp to determine the
order.
template<class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_symmetric_difference(
InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result);
template<class InputIterator1, class InputIterator2, class OutputIterator,
class Compare>
OutputIterator set_symmetric_difference(
InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Compare comp);
The set_symmetric_difference() function constructs the set that is the symmetric
difference of the ranges [first1, last1) and [first2, last2) and copies the result to the
location pointed to by result. The resulting range should not overlap either of the
original ranges. The function returns a past-the-end iterator for the constructed range.
The symmetric difference is the set containing those elements found in the first set but
not in the second and those elements found in the second set but not the first. It's the
same as the difference between the union and the intersection. The first version uses
< and the second uses the comparison object comp to determine the order.
Heap Operations
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.

A heap is a common data form with the property that the first element in a heap is the
largest. Whenever the first element is removed or any element is added, the heap may
have to be rearranged to maintain that property. A heap is designed so that these two
operations are done efficiently.
template<class RandomAccessIterator>
void make_heap(RandomAccessIterator first, RandomAccessIterator last);
template<class RandomAccessIterator, class Compare>
void make_heap(RandomAccessIterator first, RandomAccessIterator last,
Compare comp);
The make_heap() function makes a heap of the range [first, last). The first version
use < to determine the ordering, while the second version uses the comp comparison
object.
template<class RandomAccessIterator>
void push_heap(RandomAccessIterator first, RandomAccessIterator last);
template<class RandomAccessIterator, class Compare>
void push_heap(RandomAccessIterator first, RandomAccessIterator last,
Compare comp);
The push_heap() function assumes that the range [first, last - 1) is a valid heap, and it
adds the value at location last - 1 (that is, one past the end of the assumed valid heap)
into the heap, making [first, last) a valid heap. The first version use < to determine the
ordering, while the second version uses the comp comparison object.
template<class RandomAccessIterator>
void pop_heap(RandomAccessIterator first, RandomAccessIterator last);
template<class RandomAccessIterator, class Compare>
void pop_heap(RandomAccessIterator first, RandomAccessIterator last,
Compare comp);
The pop_heap() function assumes that the range [first, last) is a valid heap. It swaps
the value at location last - 1 with the value at first and makes the range [first, last - 1)
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
a valid heap. The first version uses < to determine the ordering, while the second

version uses the comp comparison object.
template<class RandomAccessIterator>
void sort_heap(RandomAccessIterator first, RandomAccessIterator last);
template<class RandomAccessIterator, class Compare>
void sort_heap(RandomAccessIterator first, RandomAccessIterator last,
Compare comp);
The sort_heap() function assumes the range [first, last) is a heap and sorts it. The
first version uses < to determine the ordering, while the second version uses the comp
comparison object.
Minimum and Maximum
The minimum and maximum functions return the minimum and maximum values of
pairs of values and of sequences of values.
template<class T> const T& min(const T& a, const T& b);
template<class T, class Compare>
const T& min(const T& a, const T& b, Compare comp);
The min() function returns the lesser of two values. If the two values are equivalent, it
returns the first value. The first version uses < to determine the ordering, while the
second version uses the comp comparison object.
template<class T> const T& max(const T& a, const T& b);
template<class T, class Compare>
const T& max(const T& a, const T& b, Compare comp);
The max() function returns the greater of two values. If the two values are equivalent,
it returns the first value. The first version uses < to determine the ordering, while the
second version uses the comp comparison object.
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×