泛型算法系列35:set_difference()&&set_intersection()&&set_symmetric_difference()&&set_union()时间:2022-01-04 19:25:26#include <algorithm> #include <set> #include <string> #include <iostream>#include <functional>using namespace std;/************************************************************************//* */template<class _InIt1, class _InIt2, class _OutIt> inline _OutIt my_Set_union(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest){ // OR sets [_First1, _Last1) and [_First2, _Last2), 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) *_Dest++ = *_First2, ++_First2; else *_Dest++ = *_First1, ++_First1, ++_First2; _Dest = _STDEXT unchecked_copy(_First1, _Last1, _Dest); return (_STDEXT unchecked_copy(_First2, _Last2, _Dest));}template<class _InIt1, class _InIt2, class _OutIt> inline _OutIt my_Set_intersection(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest){ // AND sets [_First1, _Last1) and [_First2, _Last2), using operator< _DEBUG_ORDER(_First1, _Last1); _DEBUG_ORDER(_First2, _Last2); _DEBUG_POINTER(_Dest); for (; _First1 != _Last1 && _First2 != _Last2; ) if (_DEBUG_LT(*_First1, *_First2)) ++_First1; else if (*_First2 < *_First1) ++_First2; else *_Dest++ = *_First1++, ++_First2; return (_Dest);}template<class _InIt1, class _InIt2, class _OutIt> 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<class _InIt1,class _InIt2,class _OutIt> inline _OutIt my_Set_symmetric_difference(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest){ // XOR sets [_First1, _Last1) and [_First2, _Last2), 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) *_Dest++ = *_First2, ++_First2; else ++_First1, ++_First2; _Dest = _STDEXT unchecked_copy(_First1, _Last1, _Dest); return (_STDEXT unchecked_copy(_First2, _Last2, _Dest));}/************************************************************************//* * generates: set #1 elements: Eeyore Piglet Pooh Tigger set #2 elements: Heffalump Pooh Woozles set_union() elements: Eeyore Heffalump Piglet Pooh Tigger Woozles set_intersection() elements: Pooh set_difference() elements: Eeyore Piglet Tigger set_symmetric_difference() elements: Eeyore Heffalump Piglet Tigger Woozles */int main() { string str1[] = { "Pooh", "Piglet", "Tigger", "Eeyore" }; string str2[] = { "Pooh", "Heffalump", "Woozles" }; ostream_iterator< string > ofile( cout, " " ); set<string,less<string>,allocator<string>> set1( str1, str1+4 ); set<string,less<string>,allocator<string>> set2( str2, str2+3 ); cout << "set #1 elements:/n/t"; copy( set1.begin(), set1.end(), ofile ); cout <<"/n/n"; cout << "set #2 elements:/n/t"; copy( set2.begin(), set2.end(), ofile ); cout << "/n/n"; set<string,less<string>,allocator<string>> res; //並集 my_Set_union( set1.begin(), set1.end(), set2.begin(), set2.end(), inserter( res, res.begin() )); cout << "set_union() elements:/n/t"; copy( res.begin(), res.end(), ofile ); cout << "/n/n"; res.clear(); //交集 set_intersection( set1.begin(), set1.end(), set2.begin(), set2.end(), inserter( res, res.begin() )); cout << "set_intersection() elements:/n/t"; copy( res.begin(), res.end(), ofile ); cout << "/n/n"; res.clear(); //差集 my_Set_difference( set1.begin(), set1.end(), set2.begin(), set2.end(), inserter( res, res.begin() )); cout << "set_difference() elements:/n/t"; copy( res.begin(), res.end(), ofile ); cout << "/n/n"; res.clear(); //対称差集 my_Set_symmetric_difference( set1.begin(), set1.end(), set2.begin(), set2.end(), inserter( res, res.begin() )); cout << "set_symmetric_difference() elements:/n/t"; copy( res.begin(), res.end(), ofile ); cout << "/n/n"; return 0;} 通过algorithm中提供的set_intersection、set_union、set_difference、set_symmetric_difference四个函数,可以方便的实现集合的交、并、差、对称差操作