赞
踩
- #include <iostream>
- #include<string>
- #include <vector>
- #include <list>
- #include <set>
- #include <map>
- #include <algorithm>
- #include <functional>
- using namespace std;
-
- template<typename T>
- class SumAdd
- {
- public:
- SumAdd()
- {
-
- }
- T operator()(const T &i,const T &j)
- {
- return i+j;
- }
- protected:
- private:
- };
-
- bool MyCompare(int i,int j)
- {
- return i<j;
- }
-
- void showElement(int i)
- {
- cout<<i<<" ";
- }
- void display()
- {
- vector<int> v1;
- v1.push_back(10);
- v1.push_back(4);
- v1.push_back(3);
- v1.push_back(2);
- v1.push_back(5);
- v1.push_back(7);
- v1.push_back(6);
- v1.push_back(1);
- vector<int> v2;
- v2.push_back(1);
- v2.push_back(2);
- v2.push_back(3);
- v2.push_back(4);
- v2.push_back(5);
- v2.push_back(6);
- v2.push_back(7);
- v2.push_back(10);
-
- vector<int> v3(8);
-
- vector<int> v4;
- v4.push_back(10);
- v4.push_back(4);
- v4.push_back(3);
- v4.push_back(2);
- v4.push_back(5);
- v4.push_back(7);
- v4.push_back(6);
- v4.push_back(1);
- SumAdd<int> sumadd;
- /*
- template<class _InIt1,
- class _InIt2,
- class _OutIt,
- class _Fn2> inline
- _OutIt transform(_InIt1 _First1, _InIt1 _Last1,
- _InIt2 _First2, _OutIt _Dest, _Fn2 _Func)
- { // transform [_First1, _Last1) and [_First2, ...) with _Func
- _DEBUG_RANGE(_First1, _Last1);
- _DEBUG_POINTER(_Dest);
- _DEBUG_POINTER(_Func);
- if (_First1 != _Last1)
- return (_Transform2(_Unchecked(_First1), _Unchecked(_Last1),
- _First2, _Dest, _Func,
- _Is_checked(_Dest)));
- return (_Dest);
- }
- */
- //返回值是v3.end()
- vector<int>::iterator it = transform(v1.begin(),v1.end(),v2.begin(),v3.begin(),sumadd);
-
- cout<<*(--it)<<endl;
-
- for (it = v3.begin();it!=v3.end();it++)
- {
- cout<<*it<<" ";
- }
- cout<<"----------------"<<endl;
-
- //把运算结果保存在v1里
- vector<int>::iterator it2 = transform(v1.begin(),v1.end(),v2.begin(),v1.begin(),sumadd);
-
- cout<<*(--it2)<<endl;
-
- for (it2 = v1.begin();it2!=v1.end();it2++)
- {
- cout<<*it2<<" ";
- }
-
- cout<<endl;
- //排序v4
- cout<<"sort--------------"<<endl;
- sort(v4.begin(),v4.end(),MyCompare);
- //遍历
- for_each(v4.begin(),v4.end(),showElement);
- }
- int main()
- {
- display();
- system("pause");
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。