T *
iterators.
find()
is easy, but how about find_rightmost_subsequence()
?
std::
to make it clear you're using generic algorithms.
InputIterator min_element( InputIterator beg, InputIterator end);
replace(b, e, old, new)
needs
forward iterators
.
sort(b, e)
uses
random iterators
.
<algorithm>
.
find(v), find_first_of(v), adjacent_find()
search(), search_n(), find_end()
mismatch(),equal()
count(v)
values in a sequence.
for_each(f)
applies a function to each value in a sequence.
accumulate()
inner_product()
partial_sum()
adjacent_difference()
transform(f), replace(v1, v2), fill(v), generate(f)
remove(v), unique()
reverse(), rotate(), random_shuffle(), partition()
std::vector<int> iv1(10, 0); std::vector<int> iv2; std::copy(iv1.begin(), iv1.end(), iv2.begin());
This goes (or should go) boom. Why?
std::vectoriv1(10, 0); std::vector iv2(iv1);
Know your tools.
iv2
is properly sized; the data transfer optimized.
sort(), stable_sort(), partial_sort()
nth_element(), binary_search(), lower_bound(), upper_bound(), ...
merge()
two sorted sequences into one.
set_union(), set_intersection(), ...
push_heap(), pop_heap(), make_heap(), ...
less()
is not obvious.
less()
must be a trichotomy:
If both a < b
and b < a
are false
then a == b
.
<
is a trichotomy, <=
is not.
a < b
and b < a
are false, then a == b
.
a <= b
and b <= a
are never false,a == b
is never true.
_if
convention distinguishes value-predicate algorithms.
find(start, end, value)
Return an iterator to the leftmost occurrence of value
in the sequence,
or end
if there's no such value.
find_if(start, end, predicate)
Return an iterator to the leftmost occurrence of a value in the sequence that
causes the predicate to return true, or end
if there's no such value.
_copy
convention indicates non-mutating mutating algorithms.
reverse(start, end)
Reverse the order of the elements in the given sequence.
reverse(start, end, new_start)
Copy the reversed sequence into the sequence starting at new_start
.
iv_iter i = iv.begin() while (i != iv.end()) and (iv[i] != 0) ++i
iv_iter i = std::find(iv.begin(), iv.end(), 0)
for(unsigned i = 0; i < s.size(); i++) s[i] = std::tolower(s[i])
std::transform( s.begin(), s.end(), s.begin(), std::tolower)
std::sort()
requires random iterators for efficiency.
.sort()
list member function.
std::copy(s.begin(),s.end(),d.begin())
v2.assign(s.begin(), s.end())
v2.insert(d, s.begin(), s.end())
v2.assign()
changes v2
's size; std::copy()
can't (without
help).
This page last modified on 2 March 2004.