Mega Code Archive

 
Categories / C++ / Algorithm
 

Use includes() to check for subset

#include <iostream> #include <list> #include <algorithm> using namespace std; template<class InIter> void show_range(const char *msg, InIter start, InIter end); int main() {   list<char> list1, list2, result(15), list3;   list<char>::iterator res_end;   for(int i=0; i < 5; i++)       list1.push_back('A'+i);   for(int i=3; i < 10; i++)       list2.push_back('A'+i);         show_range("Contents of list1: ", list1.begin(), list1.end());   list3.push_back('A');   list3.push_back('C');   list3.push_back('D');      if(includes(list1.begin(), list1.end(),list3.begin(), list3.end()))     cout << "list3 is a subset of list1" << endl;   else     cout << "list3 is not a subset of list1" << endl;   return 0; } template<class InIter> void show_range(const char *msg, InIter start, InIter end) {   InIter itr;   cout << msg << endl;   for(itr = start; itr != end; ++itr)     cout << *itr << endl; }