类属行transform的作用是,将某个函数作用到某一个区间内的每一个元素上,并将该函数所返回的结构保存到另一个区间中。
1
#include
<
algorithm
>
2 #include < iostream >
3 #include < iterator >
4 using namespace std;
5
6 int sum( int val1, int val2) { return val1 + val2;}
7
8 int main()
9 {
10 cout << " Illustrating the generic transform algorithm. " << endl;
11 int array1[ 5 ] = { 0 , 1 , 2 , 3 , 4 };
12 int array2[ 5 ] = { 6 , 7 , 8 , 9 , 10 };
13 ostream_iterator < int > out (cout, " " );
14
15 transform( & array1[ 0 ], & array1[ 5 ], & array2[ 0 ], out ,sum);
16 cout << endl;
17 return 0 ;
18 }
2 #include < iostream >
3 #include < iterator >
4 using namespace std;
5
6 int sum( int val1, int val2) { return val1 + val2;}
7
8 int main()
9 {
10 cout << " Illustrating the generic transform algorithm. " << endl;
11 int array1[ 5 ] = { 0 , 1 , 2 , 3 , 4 };
12 int array2[ 5 ] = { 6 , 7 , 8 , 9 , 10 };
13 ostream_iterator < int > out (cout, " " );
14
15 transform( & array1[ 0 ], & array1[ 5 ], & array2[ 0 ], out ,sum);
16 cout << endl;
17 return 0 ;
18 }
显示结果为:
Illustrating the generic transform algorithm.
6 8 10 12 14