1.算法accumulate
template<class InputIterator, class T>
T accumulate(InputIterator first, InputIterator last, T init) {
for(; first != last; ++first)
init = init + *first;
return init;
}
template<class InputIterator, class T, class BinaryOperation>
T accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op) {
for(; first != last; ++first)
init = binary_op(init, *first);
return init;
}
2.算法for_each
template<class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function f) {
for(; first != last; ++first)
f(*first);
return f;
}
3.算法replace
template<class ForwardIterator, class T>
void replace(ForwardIterator first, ForwardIterator last,
const T& old value, const T& new_value) {
for(; first!=last; ++first)
if(*first==old_value)
*first=new_value;
}
4.算法replace_if
template<class ForwardIterator, class Predicate, class T>
void replace_if(ForwardIterator first, ForwardIterator last,
Predicate pred, const T& new_value) {
for(; first!=last; ++first)
if(pred(*first))
*first=new_value;
}
5.算法replace_copy
template<class InputIterator, class OutputIterator, class T>
OutputIterator replace_copy(InputIterator first, InputIterator last,
OutputIterator result, const T& old_value, const T& new_value) {
for(; first!=last; ++first, ++result)
*result = *first==old_value?new_value:*first;
return result;
}
6.算法count
template<class InputIterator, class T>
typename iterator_traits<InputIterator>::difference_type
cout(InputIterator first, InputIterator last, const T& value) {
typename iterator_traits<InputIterator>::difference_type n = 0;
for(; first!=last; ++first)
if(*first==value)
++n
return n;
}
7.算法count_if
template<class InputIterator, class Predicate>
typename iterator_traits<InputIterator>::difference_type
count_if(InputIterator first, InputIterator last, Predicate pred) {
typename iterator_traits<InputIterator>::difference_type n = 0;
for(; first!=last; ++first)
if(pred(*first))
++n;
return n;
}
8.算法find
template<class InputIterator, class T>
InputIterator find(InputIterator first, InputIterator last, const T& value) {
while(first!=last&&*first!=value)
++first
return first;
}
9.算法find_if
template<class InputIterator, class Predicate>
InputIterator find(InputIterator first, InputIterator last, Predicate pred) {
while(first!=last&&!pred(*first))
++first
return first;
}