更易型算法的特点:
接受一个操作,该操作可以改动其所接受的实参,所以该实参必须以 by reference方式传递。
void square(int& elem) // call by reference
{
elem = elem * elem; // assign processed value directly
}
for_each(coll.begin(), coll.end(), square);
for_each源码分析
function template
std::for_each
template <class InputIterator,class Function>
for_each(InputIterator first,InputIterator last,Function fn)
Apply function to range
Applies function fn to each of elements in the range [first,last)
The behavior of this template function is equivalent to
原型
template <class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator lasr, Function fn)
{
while(first != last)
{
fn(*first);
++first;
}
return fn;
}
参数详解
first ,last
The Range used is [First, last), which contains all the elements between first and last.
fn
Unary function that accepts an element in the range as argument
this can either be a function pointer or a move constructible function object
its return value, if any ,is ignored.
transform()算法:运用某个操作,该操作返回被改动后的实参,此算法的奥妙之初就在于它可以用来将结果赋值给原元素。
我们以一元操作符为示例
int squqre(int elem) // call-by-value
{
return elem*elem; // return processed value
}
示例
trasnform(coll.begin(), coll.end(), // source range
coll.begin(), // destination range
square); // operation
源码分析
function template
std::tradnsform
一元操作符
template<calss InputIterator, class OutputIterator,class UnaryOperator>
OutoutIterator transform(InputIterator first1, InputIterator last1,OutputIterator result,UnaryOperator op)
{
while(first1 != last1)
{
// 一元操作符
*result = op(*first);
++result;
++first;
}
return result;
}
二元操作符
template<calss InputIterator, calss InputIterator2 ,class OutputIterator,class UnaryOperator>
OutoutIterator transform(InputIterator first1, InputIterator last1,InputIterator2 first2
OutputIterator result,BinaryOperation binary_op)
{
while(first1 != last1)
{
// 二元操作符
*result = binary_op(*first1,*first2);
++result;
++first;
}
return result;
}
unary operation(1)
template<class InputIterator,class OutputIterator,class UnaryOperation>
OutputIterator transform(InoutIterator first1,InoutIterator last1,OutputIterator result,UnaryOperation op);
Applies op object to each of the elements in the range [first1,last1) and stores the value returned by each operation in the range that begins at result.
binary operation(2)
template<class InputIterator1,calss InputIterator2,class OutputIterator,class BinaryOperation>
OutputIterator transform(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2, OutputIterator result , BinaryOperation binary_op)
Calls binary_op using each of the elements in the range [first1,last1) as first argument, and the respective argument in the range that begins at first2 as second atgument. the value returned by each call is stored in the range that begins at result.
案例分析:
#include
#include
#include
using namespace std;
int op_increase (int i) { return ++i;}
int op_sum (int i, int j) { return i+j; }
int main () {
vector<int> first;
vector<int> second;
vector<int>::iterator it;
// set some values:
for (int i=1; i<6; i++) first.push_back (i*10);
cout << "first contains:";
for (it=first.begin(); it!=first.end(); ++it)
cout << " " << *it;
cout << endl;
second.resize(first.size());
transform (first.begin(), first.end(), second.begin(), op_increase);
cout << "\n after op_increase first contains:";
for (it=first.begin(); it!=first.end(); ++it)
cout << " " << *it;
cout << endl;
cout << "after op_increase second contains:";
for (it=second.begin(); it!=second.end(); ++it)
cout << " " << *it;
cout << endl;
transform (first.begin(), first.end(), second.begin(), first.begin(), op_sum);
cout << "\n after op_sum first contains:";
for (it=first.begin(); it!=first.end(); ++it)
cout << " " << *it;
cout << endl;
cout << "after op_sum second contains:";
for (it=second.begin(); it!=second.end(); ++it)
cout << " " << *it;
cout << endl;
return 0;
}
copy()算法
描述:容器内指定范围的元素拷贝到另一容器中。
function template
std::Copy
Copy range of elements
1: Copies the elements in the range [first,last) into the range beginning at result.
2: The function returns an iterator to the end of the destination range.
The behavior of this function template is equivalent to
template<class InputIterator, class OutputIterator>
OutputIterator copy(InputIterator first, InputIterator last, OutputIterator reslut)
{
while(first != last)
{
*result = *first;
++result;
++first;
}
return result;
}
举例
#include
#include
#include
int main(){
int myints[] = {10,20,30};
std::vector<int> vec(3);
std::copy(myints,myints+3,vec.begin());
}
copy_if() 算法:
描述:复制那些“符合某个给定规则”的元素
function template
std::copy_if
template <class InputIterator,class OutputIterator,class UnaryPredicate>
OutputIterator copy_if(InputIterator first, InputInterator last,
OutputIterator result,UnaryPredicate pred)
{
while(first != last)
{
if(pred(*first))
{
*result = *first;
++result;
}
++first;
}
return result;
}
Example
// copy_if example
#include
#include
#include
int main()
{
std::vector<int> foo = {25,15,-5,-45};
std::vector<int> bar(foo.size());
// copy only positive numbers (只复制正数)
auto it = std::copy_if(foo.begin(),foo.end(),bar.begin(),
[](int i){
return !(i<0);
});
return 0;
}