Create the intersection between two list - C++ STL Algorithm

C++ examples for STL Algorithm:set_intersection

Description

Create the intersection between two 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()//  ww  w.  j ava  2 s  .c o  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 the intersection of lst1 and lst2.
   res_end = set_intersection(lst1.begin(), lst1.end(), lst2.begin(), lst2.end(), result.begin());
   show_range("Intersection of lst1 and 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