返回
includes,set_intersection,set_union,set_difference,set_symmetric_difference都是集合论中的操作。这些算法都假定指定区间类的元素已经有序。
includes:A∈B
set_intersection:A∩B
set_union:A∪B
set_difference:A-B
set_symmetric_difference:(A-B)∪(B-A)
声明:
#include <algorithm> template <class inputItr1, class inputItr2> bool includes(inputItr1 first1, inputItr1 last1, inputItr2 first2, inputItr2 last2); template <class inputItr1, class inputItr2, class binaryPredicate> bool includes(inputItr1 first1, inputItr1 last1, inputItr2,binaryPredicate op);
#include <iostream> #include <list> #include <string> #include <numeric> #include <iterator> #include <vector> #include <functional> #include <algorithm> using namespace std; int main() { char setA[5] = {'A','B','C','D','E'}; char setB[10] = {'A','B','C','D','E','F','I','J','K','L'}; char setC[5] = {'A','E','I','O','U'}; ostream_iterator<char> screen(cout, " "); cout << "setA:" << endl; copy(setA,setA+5,screen); cout << endl; cout << "setB:" << endl; copy(setB,setB+10,screen); cout << endl; cout << "setC:" << endl; copy(setC,setC+5,screen); cout << endl; if (includes(setB,setB+10,setA,setA+5)) { cout << "setA is a subset of setB" << endl; } else { cout << "setA is not a subset of setB" << endl; } if (includes(setB,setB+10,setC,setC+5)) { cout << "setC is a subset of setB" << endl; } else { cout << "setC is not a subset of setB" << endl; } return 0; }
charList:
a b c d e
charList:
A B C D E
list
2 8 5 1 7 11 3
4 16 10 2 14 22 6
list
4 16 10 2 14 22 6
声明:
#include <algorithm> template <class inputItr1, class inputItr2,class outputItr> outputItr set_intersection(inputItr1 first1, inputItr1 last1, inputItr2 first2,inputItr2 last2,outputItr destFirst); template <class inputItr1, class inputItr2,class outputItr,class binaryPredicate> outputItr set_intersection(inputItr1 first1, inputItr1 last1, inputItr2 first2,inputItr2 last2,outputItr destFirst,binaryPredicate op); template <class inputItr1, class inputItr2,class outputItr> outputItr set_union(inputItr1 first1, inputItr1 last1, inputItr2 first2,inputItr2 last2,outputItr destFirst); template <class inputItr1, class inputItr2,class outputItr,class binaryPredicate> outputItr set_union(inputItr1 first1, inputItr1 last1, inputItr2 first2,inputItr2 last2,outputItr destFirst,binaryPredicate op);
#include <iostream> #include <list> #include <string> #include <numeric> #include <iterator> #include <vector> #include <functional> #include <algorithm> using namespace std; int main() { int setA[5] = {2,4,5,7,8}; int setB[7] = {1,2,3,4,5,6,7}; int setC[5] = {2,5,8,8,15}; int setD[6] = {1,4,4,6,7,12}; int AunionB[10]; int AunionC[10]; int BunionD[15]; int AintersectB[10]; int AintersectC[10]; int * lastElement; ostream_iterator<int> screen(cout," "); cout << "setA:" << endl; copy(setA,setA+5,screen); cout << endl; cout << "setB:" << endl; copy(setB,setB+7,screen); cout << endl; cout << "setC:" << endl; copy(setC,setC+5,screen); cout << endl; cout << "setD:" << endl; copy(setD,setD+6,screen); cout << endl; // set_union lastElement = set_union(setA,setA+5,setB,setB+7,AunionB); cout << "AunionB:" << endl; copy(AunionB,lastElement,screen); cout << endl; lastElement = set_union(setA,setA+5,setC,setC+5,AunionC); cout << "AunionC:" << endl; copy(AunionC,lastElement,screen); cout << endl; lastElement = set_union(setB,setB+7,setD,setD+6,BunionD); cout << "BunionD:" << endl; copy(BunionD,lastElement,screen); cout << endl; // set_intersection lastElement = set_intersection(setA, setA+5,setB,setB+7,AintersectB); cout << "AintersectB:" << endl; copy(AintersectB,lastElement,screen); cout << endl; lastElement = set_intersection(setA, setA+5,setC,setC+5,AintersectC); cout << "AintersectC:" << endl; copy(AintersectC,lastElement,screen); cout << endl; return 0; }
运行结果:
charList:
a b c d e
charList:
A B C D E
list
2 8 5 1 7 11 3
4 16 10 2 14 22 6
list
4 16 10 2 14 22 6
#include <algorithm> template <class inputItr1, class inputItr2,class outputItr> outputItr set_difference(inputItr1 first1, inputItr1 last1, inputItr2 first2,inputItr2 last2,outputItr destFirst); template <class inputItr1, class inputItr2,class outputItr,class binaryPredicate> outputItr set_difference(inputItr1 first1, inputItr1 last1, inputItr2 first2,inputItr2 last2,outputItr destFirst,binaryPredicate op); template <class inputItr1, class inputItr2,class outputItr> outputItr set_symmetric_difference(inputItr1 first1, inputItr1 last1, inputItr2 first2,inputItr2 last2,outputItr destFirst); template <class inputItr1, class inputItr2,class outputItr,class binaryPredicate> outputItr set_symmetric_difference(inputItr1 first1, inputItr1 last1, inputItr2 first2,inputItr2 last2,outputItr destFirst,binaryPredicate op);
#include <iostream> #include <list> #include <string> #include <numeric> #include <iterator> #include <vector> #include <functional> #include <algorithm> using namespace std; int main() { int setA[5] = {2,4,5,7,8}; int setB[7] = {3,4,5,6,7,8,10}; int setC[5] = {1,5,6,8,15}; int AdifferenceC[5]; int BsymDiffC[10]; int * lastElement; ostream_iterator<int> screen(cout," "); cout << "setA:" << endl; copy(setA,setA+5,screen); cout << endl; cout << "setB:" << endl; copy(setB,setB+7,screen); cout << endl; cout << "setC:" << endl; copy(setC,setC+5,screen); cout << endl; // set_difference lastElement = set_difference(setA,setA+5,setC,setC+5,AdifferenceC); cout << "AdifferenceC:" << endl; copy(AdifferenceC,lastElement,screen); cout << endl; // set_symmetric_differenc 对称差 lastElement = set_symmetric_difference(setB,setB+7,setC,setC+5,BsymDiffC); cout << "BsymDiffC:" << endl; copy(BsymDiffC,lastElement,screen); cout << endl; return 0; }