泛型算法系列24:set_difference()

#pragma warning (disable:4786) #include #include #include #include using namespace std; /************************************************************************/ /* */ template inline _OutIt my_set_difference(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest) { // take set [_First2, _Last2) from [_First1, _Last1), using operator< _DEBUG_ORDER(_First1, _Last1); _DEBUG_ORDER(_First2, _Last2); _DEBUG_POINTER(_Dest); for (; _First1 != _Last1 && _First2 != _Last2; ) if (_DEBUG_LT(*_First1, *_First2)) *_Dest++ = *_First1, ++_First1; else if (*_First2 < *_First1) ++_First2; else ++_First1, ++_First2; return (_STDEXT unchecked_copy(_First1, _Last1, _Dest)); } /************************************************************************/ /* template inline insert_iterator<_Container> my_inserter(_Container& _Cont, _Iter _Where) { // return insert_iterator return (std::insert_iterator<_Container>(_Cont, _Where)); } template inline back_insert_iterator<_Container> back_inserter(_Container& _Cont) { // return a back_insert_iterator return (std::back_insert_iterator<_Container>(_Cont)); } template inline _OutIt __CLRCALL_OR_CDECL _Copy_opt(_InIt _First, _InIt _Last, _OutIt _Dest, _InOutItCat, _Nonscalar_ptr_iterator_tag, _Range_checked_iterator_tag) { // copy [_First, _Last) to [_Dest, ...), arbitrary iterators _DEBUG_RANGE(_First, _Last); for (; _First != _Last; ++_Dest, ++_First) *_Dest = *_First; return (_Dest); } */ /************************************************************************/ int main() { srand((unsigned)time(0)); ///////////////////////////////////////////////////////// set sa; set sb; set sc; int i; for(i = 0;i < 20; i++) { sa.insert(::rand()%20); sb.insert(::rand()%20); } //the last param is insert iterator. set_difference(sa.begin(), sa.end(), sb.begin(), sb.end(), insert_iterator>(sc,sc.begin()) /*inserter(sc,sc.begin())*/); copy(sa.begin(), sa.end(), ostream_iterator(cout," ")); cout << endl; copy(sb.begin(), sb.end(), ostream_iterator(cout," ")); cout << endl; copy(sc.begin(), sc.end(), ostream_iterator(cout," ")); cout << endl; ///////////////////////////////////////////////////////// int a[]={1,2,3,4,5,6,7,8,9,10}; int b[]={2,4,6,8,10,12,14,16,18,20}; int c[20]; int * end = set_difference(a,a+10,b,b+10,c);//c is Array's pointer for( int* p=c;p != end; p++ ) cout<<*p<<" "; cout< 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 } insert_iterator<_Container>& operator=( typename _Container::const_reference _Val) { // insert into container and increment stored iterator iter = container->insert(iter, _Val); ++iter; return (*this); } insert_iterator<_Container>& operator*() { // pretend to return designated value return (*this); } insert_iterator<_Container>& operator++() { // pretend to preincrement return (*this); } insert_iterator<_Container>& operator++(int) { // pretend to postincrement return (*this); } protected: _Container *container; // pointer to container typename _Container::iterator iter; // iterator into container }; /************************************************************************/

 

用于求两个集合的差集,结果集合中包含所有属于第一个集合但不属于第二个集合的元素。

你可能感兴趣的:(算法,iterator,insert,reference,class,output,C++,STL)