disjoint() algorithm: if they contain no elements in common
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
template<class InIter>
bool disjoint(InIter start, InIter end,InIter start2, InIter end2);
int main(){
list<char> list1, list2, list3;
for(int i=0; i < 5; i++)
list1.push_back('A'+i);
for(int i=6; i < 10; i++)
list2.push_back('A'+i);
for(int i=8; i < 12; i++)
list3.push_back('A'+i);
if(disjoint(list1.begin(), list1.end(), list2.begin(), list2.end()))
cout << "list1 and list2 are disjoint\n";
else
cout << "list1 and list2 are not disjoint.\n";
return 0;
}
template<class InIter>
bool disjoint(InIter start, InIter end,InIter start2, InIter end2) {
InIter itr;
for( ; start != end; ++start)
for(itr = start2; itr != end2; ++itr)
if(*start == *itr) return false;
return true;
}
Related examples in the same category