transform函数原型
1.
template<class _InIt, class _OutIt, class _Fn1> inline _OutIt transform(_InIt _First, _InIt _Last, _OutIt _Dest, _Fn1 _Func) { // transform [_First, _Last) with _Func _DEPRECATE_UNCHECKED(transform, _Dest); return (_Transform_no_deprecate(_First, _Last, _Dest, _Func)); }
2.
template<class _InIt1, class _InIt2, class _OutIt, class _Fn2> inline _OutIt transform(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _OutIt _Dest, _Fn2 _Func) { // transform [_First1, _Last1) and [_First2, ...) with _Func _DEFINE_DEPRECATE_UNCHECKED(transform); _USE_DEPRECATE_UNCHECKED(_First2); _USE_DEPRECATE_UNCHECKED(_Dest); return (_Transform_no_deprecate(_First1, _Last1, _First2, _Dest, _Func)); }
3.
#if _ITERATOR_DEBUG_ARRAY_OVERLOADS template<class _InIt, class _OutTy, size_t _OutSize, class _Fn1> inline _OutTy *transform(_InIt _First, _InIt _Last, _OutTy (&_Dest)[_OutSize], _Fn1 _Func) { // transform [_First, _Last) with _Func, array dest return (_Unchecked( _Transform_no_deprecate(_First, _Last, _Array_iterator<_OutTy, _OutSize>(_Dest), _Func))); } #endif /* _ITERATOR_DEBUG_ARRAY_OVERLOADS */
4-6.
#if _ITERATOR_DEBUG_ARRAY_OVERLOADS template<class _InIt1, class _InTy, size_t _InSize, class _OutIt, class _Fn2> inline _OutIt transform(_InIt1 _First1, _InIt1 _Last1, _InTy (&_First2)[_InSize], _OutIt _Dest, _Fn2 _Func) { // transform [_First1, _Last1) and [_First2, ...), array input _DEPRECATE_UNCHECKED(transform, _Dest); return (_Transform_no_deprecate(_First1, _Last1, _Array_iterator<_InTy, _InSize>(_First2), _Dest, _Func)); } template<class _InIt1, class _InIt2, class _OutTy, size_t _OutSize, class _Fn2> inline _OutTy *transform(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _OutTy (&_Dest)[_OutSize], _Fn2 _Func) { // transform [_First1, _Last1) and [_First2, ...), array dest _DEPRECATE_UNCHECKED(transform, _First2); return (_Unchecked( _Transform_no_deprecate(_First1, _Last1, _First2, _Array_iterator<_OutTy, _OutSize>(_Dest), _Func))); } template<class _InIt1, class _InTy, size_t _InSize, class _OutTy, size_t _OutSize, class _Fn2> inline _OutTy *transform(_InIt1 _First1, _InIt1 _Last1, _InTy (&_First2)[_InSize], _OutTy (&_Dest)[_OutSize], _Fn2 _Func) { // transform [_First1, _Last1) and [_First2, ...), array input/dest return (_Unchecked( _Transform_no_deprecate(_First1, _Last1, _Array_iterator<_InTy, _InSize>(_First2), _Array_iterator<_OutTy, _OutSize>(_Dest), _Func))); } #endif /* _ITERATOR_DEBUG_ARRAY_OVERLOADS */
暂时只研究前面2种
1.
#include <iostream> #include<algorithm> #include <functional> #include <vector> using namespace std; class Ticket { public: Ticket() { ; } int num; int id; char zimu; }; class MyShow_transform { public: Ticket* operator()(Ticket* t) { if (t->zimu == ‘A‘) { cout << "车号:" << t->num << "座位:" << t->id << "排" << t->zimu << "座" << endl; return t; } t->num = 0; return t; } }; void show(Ticket* t) { if(t->num != 0) cout<< t->num << "车" << t->id << "排" << t->zimu << "座" << endl; return; } void test_transform() { vector<Ticket*> v1,v2,v3; for (int num = 0; num < 2; num++) { for (int id = 0; id < 5; id++) { for (int i = 0; i < 5; i++) { Ticket *tmp = new Ticket; tmp->num = num+1; tmp->id = id+1; tmp->zimu = ‘A‘ + i; v1.push_back(tmp); } } } v2.resize(v1.size()); transform(v1.begin(), v1.end(), v2.begin(),MyShow_transform() ); cout << "-------------------------------------------------------------------" << endl; for_each(v2.begin(), v2.end(), show); for (vector<Ticket*>::iterator it = v1.begin(); it != v1.end(); ) { delete (*it); it = v1.erase(it); it++; } }
增加函数适配器
#include <iostream> #include<algorithm> #include <functional> #include <vector> using namespace std; class Ticket { public: Ticket() { ; } int num; int id; char zimu; }; class MyShow_transform : public std::binary_function<Ticket*, char, Ticket*> { public: Ticket* operator()(const Ticket* t, const char c)const { Ticket* p = nullptr; if (t->zimu == c) { cout << "车号:" << t->num << "座位:" << t->id << "排" << t->zimu << "座" << endl; p = const_cast<Ticket *>(t); return p; } if(p != nullptr) p->num = 0; return p; } }; void show(Ticket* t) { if (t == nullptr) return; if(t->num != 0) cout<< t->num << "车" << t->id << "排" << t->zimu << "座" << endl; return; } void test_transform() { vector<Ticket*> v1,v2,v3; for (int num = 0; num < 2; num++) { for (int id = 0; id < 5; id++) { for (int i = 0; i < 5; i++) { Ticket *tmp = new Ticket; tmp->num = num+1; tmp->id = id+1; tmp->zimu = ‘A‘ + i; v1.push_back(tmp); } } } v2.resize(v1.size()); transform(v1.begin(), v1.end(), v2.begin(),bind2nd( MyShow_transform(),‘A‘ )); cout << "-------------------------------------------------------------------" << endl; for_each(v2.begin(), v2.end(), show); for (vector<Ticket*>::iterator it = v1.begin(); it != v1.end(); ) { delete (*it); it = v1.erase(it); it++; } } void main() { test_transform(); cout << endl; cout << "hello" << endl; system("pause"); }
2.增加一个vector
#include <iostream> #include<algorithm> #include <functional> #include <vector> using namespace std; class Ticket { public: Ticket() { ; } int num; int id; char zimu; }; class MyShow_transform : public std::binary_function<Ticket*, char, Ticket*> { public: Ticket* operator()(const Ticket* t, const char c)const { Ticket* p = nullptr; if (t->zimu == c) { cout << "车号:" << t->num << "座位:" << t->id << "排" << t->zimu << "座" << endl; p = const_cast<Ticket *>(t); return p; } if(p != nullptr) p->num = 0; return p; } }; class MyShow_transformadv : public std::binary_function<Ticket*, Ticket*, Ticket*> { public: Ticket* operator()(const Ticket* t1, const Ticket* t2)const { Ticket* p = nullptr; p = const_cast<Ticket *>(t1); if (t2 == nullptr) return p; if (t1->num == t2->num) { cout << "车号:" << t1->num << "座位:" << t1->id << "排" << t1->zimu << "座" << endl; return p; } if (p != nullptr) p->num = 0; return p; } }; void show(Ticket* t) { if (t == nullptr) return; if(t->num != 0) cout<< t->num << "车" << t->id << "排" << t->zimu << "座" << endl; return; } void test_transform() { vector<Ticket*> v1,v2,v3; for (int num = 0; num < 2; num++) { for (int id = 0; id < 5; id++) { for (int i = 0; i < 5; i++) { Ticket *tmp = new Ticket; tmp->num = num+1; tmp->id = id+1; tmp->zimu = ‘A‘ + i; v1.push_back(tmp); } } } v2.resize(v1.size()); cout << "v2-------------------------------------------------------------------" << endl; transform(v1.begin(), v1.end(), v2.begin(),bind2nd( MyShow_transform(),‘A‘ )); v3.resize(v1.size()); cout << "v3-------------------------------------------------------------------" << endl; transform(v1.begin(), v1.end(), v2.begin(),v3.begin(), MyShow_transformadv()); cout << "-------------------------------------------------------------------" << endl; for (vector<Ticket*>::iterator it = v1.begin(); it != v1.end(); ) { delete (*it); it = v1.erase(it); it++; } } void main() { test_transform(); cout << endl; cout << "hello" << endl; system("pause"); }
原文地址:https://www.cnblogs.com/smh2015/p/9743191.html
时间: 2024-10-18 01:42:09