如果要想真正学好STL 提供网站 http://www.cplusplus.com/reference/algorithm/count/
template <class InputIterator, class OutputIterator> OutputIterator adjacent_difference ( InputIterator first, InputIterator last, OutputIterator result ); template <class InputIterator, class OutputIterator, class BinaryOperation> OutputIterator adjacent_difference ( InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op );
|
|
|
|
#include <iostream>
#include <functional>
#include <numeric>
using namespace std;
int myop (int x, int y) {return x+y;}
int main () {
int val[] = {1,2,3,5,9,11,12};
int result[7];
adjacent_difference (val, val+7, result);
cout << "using default adjacent_difference: ";
for (int i=0; i<7; i++) cout << result[i] << ' ';
cout << endl;
adjacent_difference (val, val+7, result, multiplies<int>());
cout << "using functional operation multiplies: ";
for (int i=0; i<7; i++) cout << result[i] << ' ';
cout << endl;
adjacent_difference (val, val+7, result, myop);
cout << "using custom function: ";
for (int i=0; i<7; i++) cout << result[i] << ' ';
cout << endl;
return 0;
}
#include <iostream>
#include <functional>
#include <numeric>
using namespace std;
int myop (int x, int y) {return x+y;}
int main () {
int val[] = {1,2,3,5,9,11,12};
int result[7];
adjacent_difference (val, val+7, result);
cout << "using default adjacent_difference: ";
for (int i=0; i<7; i++) cout << result[i] << ' ';
cout << endl;
adjacent_difference (val, val+7, result, multiplies<int>());
cout << "using functional operation multiplies: ";
for (int i=0; i<7; i++) cout << result[i] << ' ';
cout << endl;
adjacent_difference (val, val+7, result, myop);
cout << "using custom function: ";
for (int i=0; i<7; i++) cout << result[i] << ' ';
cout << endl;
return 0;
}
Output:
using default adjacent_difference: 1 1 1 2 4 2 1 using functional operation multiplies: 1 2 6 15 45 99 132 using custom function: 1 3 5 8 14 20 23 |
#include <iostream>
#include <functional>
#include <numeric>
using namespace std;
int myop (int x, int y) {return x+y;}
int main () {
int val[] = {1,2,3,5,9,11,12};
int result[7];
adjacent_difference (val, val+7, result);
cout << "using default adjacent_difference: ";
for (int i=0; i<7; i++) cout << result[i] << ' ';
cout << endl;
adjacent_difference (val, val+7, result, multiplies<int>());
cout << "using functional operation multiplies: ";
for (int i=0; i<7; i++) cout << result[i] << ' ';
cout << endl;
adjacent_difference (val, val+7, result, myop);
cout << "using custom function: ";
for (int i=0; i<7; i++) cout << result[i] << ' ';
cout << endl;
return 0;
}