Create union for two list - C++ STL Algorithm

C++ examples for STL Algorithm:set_union

Description

Create union for 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()/*w  w w  . j  av  a  2s  .  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 the union of lst1 and lst2.
   res_end = set_union(lst1.begin(), lst1.end(), lst2.begin(), lst2.end(), result.begin());
   show_range("Union 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