//adjacent_difference need to include this #include <numeric> #include <list> #include <functional> #include <iterator> #include <iostream> using namespace std; int main() { int ia[] = { 1, 1, 2, 3, 5, 8 }; list<int> ilist(ia, ia + 6); list<int> ilist_result(ilist.size()); adjacent_difference(ilist.begin(), ilist.end(), ilist_result.begin() ); // generates output: // 1 0 1 1 2 3 copy( ilist_result.begin(), ilist_result.end(), ostream_iterator<int>(cout," ")); cout << endl; adjacent_difference( ilist.begin(), ilist.end(), ilist_result.begin(),plus<int>() ); // generates output: // 1 1 2 6 15 40 copy( ilist_result.begin(), ilist_result.end(), ostream_iterator<int>(cout," ")); cout << endl; return 0; } /************************************************************************/ ① template<class _InIt, class _OutIt, class _Ty, class _InOutItCat> inline _OutIt _Adjacent_difference(_InIt _First, _InIt _Last, _OutIt _Dest, _Ty *, _InOutItCat, _Range_checked_iterator_tag) { // compute adjacent differences into _Dest _Ty _Val = *_First; for (*_Dest = _Val; ++_First != _Last; ) { // compute another difference _Ty _Tmp = *_First; *++_Dest = _Tmp - _Val; _Val = _Tmp; } return (++_Dest); } /************************************************************************/ /************************************************************************/ ② template<class _InIt, class _OutIt, class _Fn2, class _Ty, class _InOutItCat> inline _OutIt _Adjacent_difference(_InIt _First, _InIt _Last, _OutIt _Dest, _Fn2 _Func, _Ty *, _InOutItCat, _Range_checked_iterator_tag) { // compute adjacent differences into _Dest, using _Func _Ty _Val = *_First; for (*_Dest = _Val; ++_First != _Last; ) { // compute another difference _Ty _Tmp = *_First; *++_Dest = _Func(_Tmp, _Val); _Val = _Tmp; } return (++_Dest); } /************************************************************************/