◆ 常用的遍历算法:

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\n", iItem);
} void main()
{
int iArray[] = {,,,,};
vector<int> vecInt(iArray, iArray+sizeof(iArray)/sizeof(iArray[]));
//FUNC1 = for_each(vecInt.begin(), vecInt.end(), Show); // ZC: 编译通不过
FUNC2 = for_each(vecInt.begin(), vecInt.end(), Show); if (FUNC2 == Show)
printf("FUNC2 == Show\n");
else
printf("FUNC2 != Show\n"); 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 测试代码:

 #ifdef WIN32
#pragma warning (disable: 4786)
#endif #include <string>
#include <vector>
#include <set> #include <algorithm> // 算法
#include <numeric> // 算法
#include <functional> // 算法 using namespace std; int Increase (int i)
{
return i+;
} void main()
{
vector<int> vecIntA;
vecIntA.push_back();
vecIntA.push_back();
vecIntA.push_back();
vecIntA.push_back();
vecIntA.push_back(); transform(vecIntA.begin(), vecIntA.end(), vecIntA.begin(), Increase); //vecIntA : {2,4,6,8,10} int iIdx = ;
vector<int>::iterator it = vecIntA.begin();
while (it != vecIntA.end())
{
printf("[%02d] ==> %d\n", iIdx, *it);
it ++;
}
}

ZC:控制台输出 - 1:

 [00] ==> 2
[00] ==> 4
[00] ==> 6
[00] ==> 8
[00] ==> 10
Press any key to continue

ZC: vs10 测试代码:

 // alg_transform.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream> // The function object multiplies an element by a Factor
template <class Type>
class MultValue
{
private:
Type Factor; // The value to multiply by
public:
// Constructor initializes the value to multiply by
MultValue ( const Type& _Val ) : Factor ( _Val )
{} // The function call for the element to be multiplied
int operator ( ) ( Type& elem ) const
{
return elem * Factor;
}
}; template <class Type>
class MultValue2
{
private:
Type Factor; // The value to multiply by
public:
// Constructor initializes the value to multiply by
MultValue2 ( const Type& _Val ) : Factor ( _Val )
{} // The function call for the element to be multiplied
int operator ( ) ( Type& elem1, Type& elem2 ) const
{
return elem1*elem2 * Factor;
}
}; int main( )
{
using namespace std;
vector <int> v1, v2(), v3();
vector <int>::iterator Iter1, Iter2 , Iter3; // Constructing vector v1
int i;
for ( i = - ; i <= ; i++ )
{
v1.push_back( i );
} cout << "Original vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl << endl; // Modifying the vector v1 in place
transform (v1.begin ( ) , v1.end ( ) , v1.begin ( ) , MultValue<int> ( ) );
cout << "The elements of the vector v1 multiplied by 2 in place gives:"
<< "\n v1mod = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl << endl; // Using transform to multiply each element by a factor of 5
transform ( v1.begin ( ) , v1.end ( ) , v2.begin ( ) , MultValue<int> ( ) ); cout << "Multiplying the elements of the vector v1mod\n "
<< "by the factor 5 & copying to v2 gives:\n v2 = ( " ;
for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
cout << *Iter2 << " ";
cout << ")." << endl << endl; // The second version of transform used to multiply the
// elements of the vectors v1mod & v2 pairwise
// ZC: 预定义的 二元functor
//transform ( v1.begin ( ) , v1.end ( ) , v2.begin ( ) , v3.begin ( ) ,
// multiplies <int> ( ) );
// ZC: 自定义的 二元functor
transform ( v1.begin ( ) , v1.end ( ) , v2.begin ( ) , v3.begin ( ) , MultValue2<int> ( ) ); cout << "Multiplying elements of the vectors v1mod and v2 pairwise "
<< "gives:\n v3 = ( " ;
for ( Iter3 = v3.begin( ) ; Iter3 != v3.end( ) ; Iter3++ )
cout << *Iter3 << " ";
cout << ")." << endl; system("pause");
}

ZC:控制台输出 - 2:

 Original vector  v1 = ( -4 -3 -2 -1 0 1 2 ).

 The elements of the vector v1 multiplied by 2 in place gives:
v1mod = ( -8 -6 -4 -2 0 2 4 ). Multiplying the elements of the vector v1mod
by the factor 5 & copying to v2 gives:
v2 = ( -40 -30 -20 -10 0 10 20 ). Multiplying elements of the vectors v1mod and v2 pairwise gives:
v3 = ( 640 360 160 40 0 40 160 ).
请按任意键继续. . .

?.?、第6讲 PPT.?

ZC: VC6 测试代码:

ZC:控制台输出:

X

05-11 20:17