
-----------------------------------
wtd
Thu Dec 22, 2005 2:25 am

For Cornflake
-----------------------------------
#include 
#include 
#include 
#include 
#include 

using namespace std;

template 
struct _does_not_contain
{
   _does_not_contain(InputIterator start,
                     InputIterator end);

   template 
   bool operator()(const T& value);

private:
   InputIterator s, e;
};

template 
_does_not_contain
does_not_contain(InputIterator start,
                 InputIterator end);

template 
bool always_false(const T& v) { return false; }

int main()
{
   vector names[4];

   names[0].push_back("Chris");
   names[0].push_back("Bob");
   names[0].push_back("John");

   names[1].push_back("Francis");
   names[1].push_back("Cole");
   names[1].push_back("John");
   names[1].push_back("Chris");

   remove_copy_if(
      names[0].begin(), names[0].end(),
      back_inserter(names[2]),
      does_not_contain(names[1].begin(), names[1].end()));

   remove_copy_if(
      names[1].begin(), names[1].end(),
      back_inserter(names[2]),
      does_not_contain(names[0].begin(), names[0].end()));

   sort(names[2].begin(), names[2].end());
   unique_copy(names[2].begin(), names[2].end(), back_inserter(names[3]));

   copy(names[3].begin(), names[3].end(),
        ostream_iterator(cout, "\n"));

   return 0;
}

template 
_does_not_contain::_does_not_contain(InputIterator start,
                                                    InputIterator end)
: s(start), e(end)
{
}

template 
template 
bool _does_not_contain::operator()(const T& value)
{
   return find(s, e, value) == e;
}

template 
_does_not_contain
does_not_contain(InputIterator start,
                 InputIterator end)
{
   return _does_not_contain(start, end);
}


When this is run, the output is:

Chris
John

The STL at work.

But, hey, that's a fair deal of work.

#include 
#include 
#include 
#include 
#include 

using namespace std;

int main()
{
   vector names[3];

   names[0].push_back("John");
   names[0].push_back("Chris");
   names[0].push_back("Bob");

   names[1].push_back("Francis");
   names[1].push_back("Cole");
   names[1].push_back("John");
   names[1].push_back("Chris");

   set_intersection(names[0].begin(), names[0].end(),
                    names[1].begin(), names[1].end(),
                    back_inserter(names[2]));
   sort(names[2].begin(), names[2].end());

   copy(names[2].begin(), names[2].end(),
        ostream_iterator(cout, "\n"));

   return 0;
}


-----------------------------------
md
Thu Dec 22, 2005 2:26 pm


-----------------------------------
Awesome! Thanks wtd! Unfortunately I don't have access to my workstation from home; but once I get back to waterloo I'll check it out.
