Create a list from subtracting one list from another list - C++ STL Algorithm

C++ examples for STL Algorithm:set_difference

Description

Create a list from subtracting one list from another list

Demo Code

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
template<class InIter>
void show_range(const char *msg, InIter start, InIter end);
int main()/*  w w  w.  ja  va  2  s . co  m*/
{
   list<char> lst1, lst2, result(15), lst3;
   list<char>::iterator res_end;
   for(int i=0; i < 5; i++)
      lst1.push_back('A'+i);
   for(int i=3; i < 10; i++)
      lst2.push_back('A'+i);
   show_range("Contents of lst1: ", lst1.begin(), lst1.end());
   cout << endl;
   show_range("Contents of lst2: ", lst2.begin(), lst2.end());
   cout << endl;
   // Create a set that contains lst1 - lst2.
   res_end = set_difference(lst1.begin(), lst1.end(), lst2.begin(), lst2.end(), result.begin());
   show_range("lst1 - lst2: ", result.begin(), res_end);
   cout << endl;
   return 0;
}
// Show a range of elements.
template<class InIter>
void show_range(const char *msg, InIter start, InIter end) {
   InIter itr;
   cout << msg;
   for(itr = start; itr != end; ++itr)
      cout << *itr << " ";
   cout << endl;
}

Result


Related Tutorials