for_each()
for_each: 用指定函数依次对指定范围内所有元素进行迭代访问。该函数不得修改
序列中的元素。
函数定义。for_each(begin, end, func);
template<class _InIt,
class _Fn1> inline
_Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func)
{ // perform function for each element
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Func);
return (_For_each(_Unchecked(_First), _Unchecked(_Last), _Func));
}
注意 for_each 的第三个参数 函数对象做函数参数,函数对象做返回值
class CMyShow
{
public:
CMyShow()
{
num = 0;
}
void operator()(const int &iItem)
{
num ++;
cout << iItem;
}
void printCount()
{
cout << "num:" << num << endl;
}
private:
int num;
};
void show(const int &iItem)
{
cout << iItem;
}
main()
{
int iArray[] = {0,1,2,3,4};
vector<int> vecInt(iArray,iArray+sizeof(iArray)/sizeof(iArray[0]));
for_each(vecInt.begin(), vecInt.end(), show);
//结果打印出 0 1 2 3 4
CMyShow show1 = for_each(vecInt.begin(), vecInt.end(), CMyShow());
cout << endl;
show1.printCount(); //显示对象被调用的次数
}
实例:
#include<iostream>
using namespace std;
#include "list"
#include "vector"
#include "string"
#include "algorithm"
#include "set"
#include "functional"
void printV(vector<int> &v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void printList(list<int> &v)
{
for (list<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//回调函数
void showElem(int &n)
{
cout << n << " ";
}
//函数对象
class MyShow
{
public:
MyShow()
{
num = 0;
}
void operator()(int &v)
{
num++;
cout << v << " ";
}
void printNum()
{
cout << "num: " << num << endl;
}
private:
int num;
};
void main41_foreach()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
printV(v1);
//第三个参数是回调函数入口地址
for_each(v1.begin(), v1.end(), showElem);
cout << endl;
//第三个参数是函数对象
MyShow My1 = for_each(v1.begin(), v1.end(), MyShow()); //有三个元素所以调用3次 num = 3
cout << endl;
My1.printNum();
/*
template<class _InIt,
class _Fn1> inline
_Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func)
{ // perform function for each element
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Func);
return (_For_each(_Unchecked(_First), _Unchecked(_Last), _Func));
}注意 for_each 的第三个参数 函数对象做函数参数,函数对象做返回值
*/
MyShow mya;
MyShow My2 = for_each(v1.begin(), v1.end(), mya);//给My2初始化 mya是实参 _Fn1 _Func是形参 会调用_Fn1类的拷贝函数 只是拷贝一份
cout << endl;
mya.printNum(); //mya和My2是两个不同的的对象
My2.printNum();
My2 = for_each(v1.begin(), v1.end(), mya); //给My2赋值 匿名对象的去和留
My2.printNum();
}
void main()
{
main41_foreach();
system("pause");
}
结果:
transform()
transform: 与 for_each 类似,遍历所有元素,都可对容器的元素进行修改
transform()算法有两种形式:
transform(b1, e1, b2, op)
transform(b1, e1, b2, b3, op)
template<class _InIt,
class _OutIt,
class _Fn1> inline
_OutIt transform(_InIt _First, _InIt _Last, _OutIt _Dest, _Fn1 _Func)
transform()的作用:
例如:可以一个容器的元素,通过 op,变换到另一个容器中(同一个容器中)
也可以把两个容器的元素,通过 op,变换到另一个容器中
案例:
int increase(int i)
{
return i + 100;
}
void main42_transform()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
printV(v1);
/*
template<class _InIt,
class _OutIt,
class _Fn1> inline
_OutIt transform(_InIt _First, _InIt _Last,
_OutIt _Dest, _Fn1 _Func)
{ // transform [_First, _Last) with _Func
_DEBUG_RANGE_PTR(_First, _Last, _Dest);
_DEBUG_POINTER_IF(_First != _Last, _Func);
return (_Transform1(_Unchecked(_First), _Unchecked(_Last),
_Dest, _Func, _Is_checked(_Dest)));
}
*/
//使用回调函数
transform(v1.begin(), v1.end(), v1.begin(), increase);
printV(v1);
//使用预定义函数对象
transform(v1.begin(), v1.end(), v1.begin(), negate<int>()); //转换成负数
printV(v1);
//使用函数适配器
list<int> mylist;
mylist.resize(v1.size()); //分配空间
transform(v1.begin(), v1.end(), mylist.begin(), bind2nd(multiplies<int>(), 10)); //vector中的值乘以10 转换到list里面
printList(mylist);
//transform也可以转化的结果直接输出到屏幕 使用输出迭代器ostream_iterator
transform(v1.begin(), v1.end(), ostream_iterator<int>(cout , " "), negate<int>()); //vector中的值乘以10 转换到list里面
}
void main()
{
//main41_foreach();
main42_transform();
system("pause");
}