64,282
社区成员
发帖
与我相关
我的任务
分享
#include <vector>
#include <iostream>
using namespace std;
//不是用编译器自带的for_each
template<class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function f)
{
for ( ; first!=last; ++first ) f(*first);
cout << "for_each return" << endl;
return f;
}
class A
{
public:
A(){cout << "A()" << endl;}
// A(const A&){cout << "A(const A&)" << endl;}
~A(){cout << "~A()" << endl;}
void operator()(int& k)
{
k++;
cout <<k << endl;
}
};
int main()
{
vector<int> a(3);
for_each(a.begin(), a.end(), A());
return 0;
}
#include <vector>
#include <iostream>
using namespace std;
template<class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function f)
{
for ( ; first!=last; ++first ) f(*first);
cout << "for_each return" << endl;
return f;
}
class test
{
public:
test(){}
test(const test&){}//自定义的复制构造函数
};
class A
{
public:
A(){cout << "A()" << endl;}
// A(const A&){cout << "A(const A&)" << endl;}
~A(){cout << "~A()" << endl;}
void operator()(int& k)
{
k++;
cout <<k << endl;
}
private:
test t;
};
int main()
{
vector<int> a(3);
for_each(a.begin(), a.end(), A());
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
class A
{
public:
A()
{
cout <<"A()" << endl;
}
~A()
{
cout <<"~A()" << endl;
}
void operator()(int& k)
{
k++;
cout <<k << endl;
}
};
int main()
{
vector<int> a(3);
for_each(a.begin(),a.end(),A());
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
class A
{
public:
A()
{
cout <<"A()" << endl;
}
~A()
{
cout <<"~A()" << endl;
}
void operator()(int& k)
{
k++;
cout <<k << endl;
}
private:
string str;
};
int main()
{
vector<int> a(3);
for_each(a.begin(),a.end(),A());
return 0;
}
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));
}
template<class _InIt,
class _Fn1> inline
_Fn1 _For_each(_InIt _First, _InIt _Last, _Fn1 _Func)
{ // perform function for each element
for (; _First != _Last; ++_First)
_Func(*_First);
return (_Func);
}
template<class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function f)
{
for ( ; first!=last; ++first ) f(*first);
return f;
}
#include <algorithm>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class A
{
public:
A():str("A()"){cout << "A()" << endl;}
A(const A&):str("A(const A&)"){cout << "A(const A&)" << endl;}
~A(){cout << "~A() = " << str << endl;}
void operator()(int& k)
{
k++;
cout <<k << endl;
}
private:
string str;
};
int main()
{
vector<int> a(3);
for_each(a.begin(), a.end(), A());
return 0;
}
//玩人,还是玩代码?