Use includes() to check for subset : includes « STL Algorithms Merge « C++






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;
}
  
    
  








Related examples in the same category

1.Container includes: generic includes algorithm
2.Determine whether one set is completely contained in another set
3.Use includes and search to check whether all elements in search are also in coll
4.Use includes() function to check if one set contains another set