#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<functional>
#include<algorithm>
using namespace std;
void printV(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
cout << *it << " ";
}
void printL(list<int>&l)
{
for (list<int>::iterator it = l.begin(); it != l.end(); it++)
cout << *it << " ";
}
void showElemt(int &t)
{
cout << t << " ";
}
class myShow
{
public:
myShow()
{
m_num = 0;
}
void operator()(int&t)
{
m_num++;
cout << t << " ";
}
void printNum()
{
cout << m_num << endl;
}
private:
int m_num;
};
void main41_for_each()
{
vector<int>v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
//使用函数输出v1
printV(v1);
cout << endl;
//for_each使用回调函数输出v1
for_each(v1.begin(), v1.end(), showElemt);
cout << endl;
//for_each使用自定义函数对象输出v1
for_each(v1.begin(), v1.end(), myShow());
cout << endl;
/*
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);
_For_each(_Unchecked(_First), _Unchecked(_Last), _Func);
return (_STD move(_Func));
}
*/
//查看调用次数,发现for_each形参传递的是值
myShow my1;
myShow my2 = for_each(v1.begin(), v1.end(), my1);
cout << endl;
my1.printNum();
my2.printNum();
}
int increase(int &t)
{
return t + 100;
}
int Sum(int &t1, int& t2)
{
return t1 + t2;
}
void main42_transform()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
printV(v1);
cout << endl;
//transform使用回调函数
transform(v1.begin(), v1.end(), v1.begin(),increase);
printV(v1);
cout << endl;
//transform使用预定义的函数对象
transform(v1.begin(), v1.end(), v1.begin(), negate<int>());
printV(v1);
cout << endl;
//transform使用函数适配器和函数对象
list<int>l(3);
transform(v1.begin(), v1.end(), l.begin(), bind2nd(multiplies<int>(), 10));
printL(l);
cout << endl;
//transform也可以把运算结果直接输出到屏幕
transform(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "), negate<int>());
cout << endl;
//transform 使用回调函数将v1 v2相加放入v3
vector<int> v2;
v2.push_back(2);
v2.push_back(4);
v2.push_back(6);
vector<int> v3(3);
transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), Sum);
printV(v3);
}
int showElemt2(int &t)
{
cout << t << " ";
return t;
}
void main43_transform_pk_for_each()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
vector<int>v2;
v2.push_back(2);
v2.push_back(4);
v2.push_back(6);
for_each(v1.begin(), v1.end(), showElemt);
cout << endl;
//transform 对函数调用的要求
/*
c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm(1026):
参见对正在编译的函数 模板 实例化“
_OutIt std::_Transform1<int*,_OutIt,void(__cdecl *)(int &)>(_InIt,_InIt,_OutIt,_Fn1,std::true_type)
”的引用
1> with
1> [
1> _OutIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>
1> , _InIt=int *
1> , _Fn1=void (__cdecl *)(int &)
1> ]
*/
/*
template<class _InIt,
class _OutIt,
class _Fn1> inline
_OutIt _Transform(_InIt _First, _InIt _Last,
_OutIt _Dest, _Fn1 _Func)
{ // transform [_First, _Last) with _Func
for (; _First != _Last; ++_First, ++_Dest)
*_Dest = _Func(*_First); //解释了 transform为什么 要有返回值
return (_Dest);
}
*/
transform(v2.begin(), v2.end(), v2.begin(), showElemt2);
cout << endl;
}
//结论:
//1.一般情况下,for_each所使用的函数对象,参数是引用,没有返回值;所以for_each速度快、不灵活
//2.transform所使用的函数对象,参数一般不使用引用,而且还有返回值;transform速度慢,非常灵活
STL的for_each和transform
最新推荐文章于 2024-02-24 16:09:24 发布