1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
| #include <iostream> using namespace std;
#include "string" #include <vector> #include <list> #include "set" #include <algorithm> #include "functional" #include "iterator" //输出流迭代器的头文件
void printV(vector<int> &v) { for (vector<int>::iterator it=v.begin(); it!=v.end(); it++) { cout << *it << " "; } }
void printList(list<int> &v) { for (list<int>::iterator it=v.begin(); it!=v.end(); it++) { cout << *it << " "; } }
void showElem(int &n) { cout << n << " "; }
class CMyShow { public: CMyShow() { num = 0; } void operator()(int &n) { num ++; cout << n << " "; } void printNum() { cout << "num:" << num << endl; } protected: private: int num; };
void main41_foreach() { vector<int> v1; v1.push_back(1); v1.push_back(3); v1.push_back(5);
printV(v1); 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); return (_For_each(_Unchecked(_First), _Unchecked(_Last), _Func)); } */
//函数对象 回调函数入口地址 for_each(v1.begin(), v1.end(), showElem); cout << endl;
for_each(v1.begin(), v1.end(), CMyShow()); cout << endl;
CMyShow mya; CMyShow my1 = for_each(v1.begin(), v1.end(),mya); //给my1初始化 mya.printNum(); //ma1和my1 是两个不同的对象 my1.printNum();
my1 = for_each(v1.begin(), v1.end(),mya); //给my1赋值 my1.printNum(); }
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); 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> mylist; mylist.resize( v1.size() );
transform(v1.begin(), v1.end(), mylist.begin(), bind2nd( multiplies<int>(), 10 ) ); printList(mylist); cout << endl;
//transform 也可以把运算结果 直接输出到屏幕 transform(v1.begin(), v1.end(), ostream_iterator<int>(cout, " " ), negate<int>() ); cout << endl; }
//一般情况下:for_each所使用的函数对象,参数是引用,没有返回值 //transform所使用的函数对象,参数一般不使用引用,而是还有返回值 int showElem2(int n) { cout << n << " "; return n; }
void main43_transform_pk_foreach() { vector<int> v1; v1.push_back(1); v1.push_back(3); v1.push_back(5);
vector<int> v2 = v1;
for_each(v1.begin(), v1.end(), showElem);
//transform 对 函数对象的要求 /* c:\program files\microsoft visual studio 10.0\vc\include\algorithm(1119): 参见对正在编译的函数 模板 实例化 “_OutIt std::_Transform1<int*,_OutIt, void(__cdecl *)(int &)>(_InIt,_InIt,_OutIt,_Fn1, std::tr1::true_type)”的引用 1> with 1> [ 1> _OutIt=std::_Vector_iterator<std::_Vector_val<int,std::allocator<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); //解释了 为什么 要有返回值 return (_Dest); } */ transform(v2.begin(), v2.end(), v2.begin(), showElem2); }
void main() { //main41_foreach(); //main42_transform(); // main43_transform_pk_foreach(); cout<<"hello..."<<endl; system("pause"); return ; }
|