???????????ò???(c++???е?????С????)??????????STL??????汾????o???????????????????class????operator()??????????????o?????
#include <iostream>
using namespace std;
template<class T>
struct Plus
{
T operator()(const T& x?? const T& y)const
{
return x + y;
}
};
template<class T>
struct Minus
{
T operator()(const T& x?? const T& y)const
{
return x - y;
}
};
int main()
{
Plus<int>plusobj;
Minus<int>minusobj;
cout << plusobj(3?? 4) << endl;
cout << minusobj(3?? 4) << endl;
//???????????o??????????????????
cout << Plus<int>()(43?? 50) << endl;
cout << Minus<int>()(43?? 50) << endl;
system("pause");
return 0;
}
???????????????????????????????????????С?????????????????stl??????????????o???????????????
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/*
template <class InputIterator??class Function>
Function for_each(InputIterator first?? InputIterator last?? Function f)
{
for (; first != last; ++first)
{
f(*first);
}
return f;
}
*/
template <typename T>
class print
{
public:
void operator()(const T& elem)
{
cout << elem << ' ' << endl;
}
};
int main()
{
int ia[6] = { 0??1??2??3??4??5 };
vector<int>iv(ia?? ia + 6);
for_each(iv.begin()?? iv.end()?? print<int>());
system("pause");
return 0;
}