Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 For Cornflake
Index -> Programming, C++ -> C++ Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Thu Dec 22, 2005 2:25 am   Post subject: For Cornflake

code:
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
#include <iostream>

using namespace std;

template <typename InputIterator>
struct _does_not_contain
{
   _does_not_contain(InputIterator start,
                     InputIterator end);

   template <typename T>
   bool operator()(const T& value);

private:
   InputIterator s, e;
};

template <typename InputIterator>
_does_not_contain<InputIterator>
does_not_contain(InputIterator start,
                 InputIterator end);

template <typename T>
bool always_false(const T& v) { return false; }

int main()
{
   vector<string> 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<string>(cout, "\n"));

   return 0;
}

template <typename InputIterator>
_does_not_contain<InputIterator>::_does_not_contain(InputIterator start,
                                                    InputIterator end)
: s(start), e(end)
{
}

template <typename InputIterator>
template <typename T>
bool _does_not_contain<InputIterator>::operator()(const T& value)
{
   return find(s, e, value) == e;
}

template <typename InputIterator>
_does_not_contain<InputIterator>
does_not_contain(InputIterator start,
                 InputIterator end)
{
   return _does_not_contain<InputIterator>(start, end);
}


When this is run, the output is:

code:
Chris
John


The STL at work.

But, hey, that's a fair deal of work.

code:
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
#include <iostream>

using namespace std;

int main()
{
   vector<string> 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<string>(cout, "\n"));

   return 0;
}
Sponsor
Sponsor
Sponsor
sponsor
md




PostPosted: Thu Dec 22, 2005 2:26 pm   Post subject: (No subject)

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.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: