◆ 常用的遍历算法:
1.1、用指定函数依次对指定范围内所有元素进行迭代访问。该函数不得修改序列中的元素
functor for_each(iteratorBegin, iteratorEnd, functor对每个元素进行操作);
1.2、与for_each类似,遍历所有元素,但可对容器的元素进行修改( transform 是变换的意思)
OutputIterator transform(InputIterator _First1, InputIterator _Last1, OutputIterator _Result, 一元functor对每个元素进行操作);
OutputIterator transform(InputIterator1 _First1, InputIterator1 _Last1, InputIterator2 _First2, OutputIterator _Result, 二元functor对每个元素进行操作);
1、
1.1、第6讲 PPT.42
◆ for_each() : 用指定函数依次对指定范围内所有元素进行迭代访问。该函数不得修改序列中的元素。
ZC: 只有一种参数格式,返回值为 传入的functor 。
ZC: vs2010 测试代码:
#ifdef WIN32 #pragma warning (disable: 4786) #endif #include <string> #include <vector> #include <set> #include <algorithm> // 算法 #include <numeric> // 算法 #include <functional> // 算法 using namespace std; typedef void (*FUNC1)(const int &_iItem); void (*FUNC2)(const int &_iItem); void Show(const int &iItem) { //cout << iItem; printf("iItem : %d ", iItem); } void main() { int iArray[] = {0,1,2,3,4}; vector<int> vecInt(iArray, iArray+sizeof(iArray)/sizeof(iArray[0])); //FUNC1 = for_each(vecInt.begin(), vecInt.end(), Show); // ZC: 编译通不过 FUNC2 = for_each(vecInt.begin(), vecInt.end(), Show); if (FUNC2 == Show) printf("FUNC2 == Show "); else printf("FUNC2 != Show "); system("pause"); }
ZC:控制台输出:
iItem : 0
iItem : 1
iItem : 2
iItem : 3
iItem : 4
FUNC2 == Show
请按任意键继续. . .
1.2、第6讲 PPT.42
◆ transform() : 与for_each类似,遍历所有元素,但可对容器的元素进行修改
ZC: 2种参数格式,返回值为 输出容器中 经过转换后的最后一个元素的iterator 。
ZC: VC6 测试代码:
1 #ifdef WIN32 2 #pragma warning (disable: 4786) 3 #endif 4 5 #include <string> 6 #include <vector> 7 #include <set> 8 9 #include <algorithm> // 算法 10 #include <numeric> // 算法 11 #include <functional> // 算法 12 13 using namespace std; 14 15 int Increase (int i) 16 { 17 return i+1; 18 } 19 20 void main() 21 { 22 vector<int> vecIntA; 23 vecIntA.push_back(1); 24 vecIntA.push_back(3); 25 vecIntA.push_back(5); 26 vecIntA.push_back(7); 27 vecIntA.push_back(9); 28 29 transform(vecIntA.begin(), vecIntA.end(), vecIntA.begin(), Increase); //vecIntA : {2,4,6,8,10} 30 31 int iIdx = 0; 32 vector<int>::iterator it = vecIntA.begin(); 33 while (it != vecIntA.end()) 34 { 35 printf("[%02d] ==> %d ", iIdx, *it); 36 it ++; 37 } 38 }
ZC:控制台输出 - 1:
1 [00] ==> 2 2 [00] ==> 4 3 [00] ==> 6 4 [00] ==> 8 5 [00] ==> 10 6 Press any key to continue
ZC: vs10 测试代码:
1 // alg_transform.cpp 2 // compile with: /EHsc 3 #include <vector> 4 #include <algorithm> 5 #include <functional> 6 #include <iostream> 7 8 // The function object multiplies an element by a Factor 9 template <class Type> 10 class MultValue 11 { 12 private: 13 Type Factor; // The value to multiply by 14 public: 15 // Constructor initializes the value to multiply by 16 MultValue ( const Type& _Val ) : Factor ( _Val ) 17 {} 18 19 // The function call for the element to be multiplied 20 int operator ( ) ( Type& elem ) const 21 { 22 return elem * Factor; 23 } 24 }; 25 26 template <class Type> 27 class MultValue2 28 { 29 private: 30 Type Factor; // The value to multiply by 31 public: 32 // Constructor initializes the value to multiply by 33 MultValue2 ( const Type& _Val ) : Factor ( _Val ) 34 {} 35 36 // The function call for the element to be multiplied 37 int operator ( ) ( Type& elem1, Type& elem2 ) const 38 { 39 return elem1*elem2 * Factor; 40 } 41 }; 42 43 int main( ) 44 { 45 using namespace std; 46 vector <int> v1, v2(7), v3(7); 47 vector <int>::iterator Iter1, Iter2 , Iter3; 48 49 // Constructing vector v1 50 int i; 51 for ( i = -4 ; i <= 2 ; i++ ) 52 { 53 v1.push_back( i ); 54 } 55 56 cout << "Original vector v1 = ( " ; 57 for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) 58 cout << *Iter1 << " "; 59 cout << ")." << endl << endl; 60 61 // Modifying the vector v1 in place 62 transform (v1.begin ( ) , v1.end ( ) , v1.begin ( ) , MultValue<int> ( 2 ) ); 63 cout << "The elements of the vector v1 multiplied by 2 in place gives:" 64 << " v1mod = ( " ; 65 for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) 66 cout << *Iter1 << " "; 67 cout << ")." << endl << endl; 68 69 // Using transform to multiply each element by a factor of 5 70 transform ( v1.begin ( ) , v1.end ( ) , v2.begin ( ) , MultValue<int> ( 5 ) ); 71 72 cout << "Multiplying the elements of the vector v1mod " 73 << "by the factor 5 & copying to v2 gives: v2 = ( " ; 74 for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ ) 75 cout << *Iter2 << " "; 76 cout << ")." << endl << endl; 77 78 // The second version of transform used to multiply the 79 // elements of the vectors v1mod & v2 pairwise 80 // ZC: 预定义的 二元functor 81 //transform ( v1.begin ( ) , v1.end ( ) , v2.begin ( ) , v3.begin ( ) , 82 // multiplies <int> ( ) ); 83 // ZC: 自定义的 二元functor 84 transform ( v1.begin ( ) , v1.end ( ) , v2.begin ( ) , v3.begin ( ) , MultValue2<int> ( 2 ) ); 85 86 87 cout << "Multiplying elements of the vectors v1mod and v2 pairwise " 88 << "gives: v3 = ( " ; 89 for ( Iter3 = v3.begin( ) ; Iter3 != v3.end( ) ; Iter3++ ) 90 cout << *Iter3 << " "; 91 cout << ")." << endl; 92 93 system("pause"); 94 }
ZC:控制台输出 - 2:
1 Original vector v1 = ( -4 -3 -2 -1 0 1 2 ). 2 3 The elements of the vector v1 multiplied by 2 in place gives: 4 v1mod = ( -8 -6 -4 -2 0 2 4 ). 5 6 Multiplying the elements of the vector v1mod 7 by the factor 5 & copying to v2 gives: 8 v2 = ( -40 -30 -20 -10 0 10 20 ). 9 10 Multiplying elements of the vectors v1mod and v2 pairwise gives: 11 v3 = ( 640 360 160 40 0 40 160 ). 12 请按任意键继续. . .
?.?、第6讲 PPT.?
◆
ZC: VC6 测试代码:
ZC:控制台输出:
X