#include <algorithm> #include <vector> #include <iostream> #include <functional> using namespace std; /************************************************************************/ /* */ template<class _FwdIt, class _Pr, class _Ty> inline void my_Replace_if(_FwdIt _First, _FwdIt _Last, _Pr _Pred, const _Ty& _Val) { // replace each satisfying _Pred with _Val _DEBUG_RANGE(_First, _Last); _DEBUG_POINTER(_Pred); for (; _First != _Last; ++_First) if (_Pred(*_First)) *_First = _Val; } template<class _InIt, class _OutIt, class _Pr, class _Ty> inline _OutIt my_Replace_copy_if(_InIt _First, _InIt _Last, _OutIt _Dest, _Pr _Pred, const _Ty& _Val) { // copy replacing each satisfying _Pred with _Val _DEBUG_RANGE(_First, _Last); _DEBUG_POINTER(_Dest); _DEBUG_POINTER(_Pred); for (; _First != _Last; ++_First, ++_Dest) *_Dest = _Pred(*_First) ? _Val : *_First; return (_Dest); } /************************************************************************/ /* * generates: original element sequence: 0 1 1 2 3 5 8 13 21 34 sequence after applying replace_if < 10 with 0: 0 0 0 0 0 0 0 13 21 34 sequence after applying replace_if even with 0: 0 1 1 0 3 5 0 13 21 0 */ class EvenValue { public: bool operator()( int value ) { return value % 2 ? false : true; } }; int main() { int new_value = 0; int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 }; vector< int > vec( ia, ia + 10 ); ostream_iterator< int > ofile( cout, " " ); cout << "original element sequence:/n"; copy( ia, ia + 10, ofile ); cout << '/n'; my_Replace_if( &ia[0], &ia[10], bind2nd(less<int>(),10), new_value ); cout << "sequence after applying replace_if < 10 with 0:/n"; copy( ia, ia + 10, ofile ); cout << '/n'; vector<int> vec2; my_Replace_copy_if(vec.begin(),vec.end(), back_inserter(vec2) , EvenValue(), new_value); cout << "sequence after applying replace_if even with 0:/n"; copy( vec2.begin(), vec2.end(), ofile ); cout << '/n'; return 0; }
replace()&&replace_copy()
#include <algorithm> #include <vector> #include <iostream> #include <string> using namespace std; /************************************************************************/ /* */ template<class _FwdIt, class _Ty> inline void my_Replace(_FwdIt _First, _FwdIt _Last, const _Ty& _Oldval, const _Ty& _Newval) { // replace each matching _Oldval with _Newval _DEBUG_RANGE(_First, _Last); for (; _First != _Last; ++_First) if (*_First == _Oldval) *_First = _Newval; } template<class _InIt, class _OutIt, class _Ty> inline _OutIt my_Replace_copy(_InIt _First, _InIt _Last, _OutIt _Dest, const _Ty& _Oldval, const _Ty& _Newval) { // copy replacing each matching _Oldval with _Newval _DEBUG_RANGE(_First, _Last); _DEBUG_POINTER(_Dest); for (; _First != _Last; ++_First, ++_Dest) *_Dest = *_First == _Oldval ? _Newval : *_First; return (_Dest); } /************************************************************************/ /* template<class _Category, class _Ty, class _Diff = ptrdiff_t, class _Pointer = _Ty *, class _Reference = _Ty&> struct iterator : public _Iterator_base_universal { // base type for all iterator classes typedef _Category iterator_category; typedef _Ty value_type; typedef _Diff difference_type; typedef _Diff distance_type; // retained typedef _Pointer pointer; typedef _Reference reference; }; */ template<class _Container> class my_insert_iterator : public _Outit { // wrap inserts into container as output iterator public: typedef _Container container_type; typedef typename _Container::reference reference; typedef _Range_checked_iterator_tag _Checked_iterator_category; my_insert_iterator(_Container& _Cont, typename _Container::iterator _Where) : container(&_Cont), iter(_Where) { // construct with container and iterator } my_insert_iterator<_Container>& operator=( typename _Container::const_reference _Val) { // insert into container and increment stored iterator iter = container->insert(iter, _Val); ++iter; return (*this); } /*↓↓↓ this is back_insert_iterator ↓↓↓ back_insert_iterator<_Container>& operator=(typename _Container::const_reference _Val) { // push value into container container->push_back(_Val); return (*this); } ↑↑↑ this is back_insert_iterator ↑↑↑*/ /*↓↓↓ this is front_insert_iterator ↓↓↓ front_insert_iterator<_Container>& operator=( typename _Container::const_reference _Val) { // push value into container container->push_front(_Val); return (*this); } ↑↑↑ this is front_insert_iterator ↑↑↑*/ my_insert_iterator<_Container>& operator*() { // pretend to return designated value return (*this); } my_insert_iterator<_Container>& operator++() { // pretend to preincrement return (*this); } my_insert_iterator<_Container>& operator++(int) { // pretend to postincrement return (*this); } protected: _Container *container; // pointer to container typename _Container::iterator iter; // iterator into container }; template<class _Container, class _Iter> inline my_insert_iterator<_Container> my_inserter(_Container& _Cont, _Iter _Where) { // return insert_iterator return (my_insert_iterator<_Container>(_Cont, _Where)); } /************************************************************************/ /************************************************************************/ /* generates: original element sequence: Christopher Robin Mr. Winnie the Pooh Piglet Tigger Eeyore sequence after applying replace(): Christopher Robin Pooh Piglet Tigger Eeyore sequence after applying replace_copy(): Christopher Robin Mr. Winnie the Pooh Piglet Tigger Eeyore */ /************************************************************************/ /* */ /************************************************************************/ int main() { string oldval( "Mr. Winnie the Pooh" ); string newval( "Pooh" ); ostream_iterator<string> ofile( cout," " ); string sa[] = { "Christopher Robin", "Mr. Winnie the Pooh", "Piglet", "Tigger", "Eeyore" }; vector< string > vec( sa, sa + 5 ); cout << "original element sequence:/n"; copy( vec.begin(), vec.end(), ofile ); cout << '/n'; my_Replace( vec.begin(), vec.end(), oldval, newval ); cout << "sequence after applying replace():/n"; copy( vec.begin(), vec.end(), ofile ); cout << '/n'; vector< string > vec2; /************************************************************************************/ /* back_inserter(vec2) AND inserter( vec2, vec2.begin() ) */ /* we can't use front_inserter(vec2) because in vector contain can't find push_front*/ /************************************************************************************/ my_Replace_copy( vec.begin(), vec.end(), my_inserter(vec2, vec2.begin()), newval, oldval ); cout << "sequence after applying replace_copy():/n"; copy( vec2.begin(), vec2.end(), ofile ); cout << '/n'; return 0; }