set_union, set_difference, set_symmetric_difference, set_intersection : Vector Utility « Data Structure « C++






set_union, set_difference, set_symmetric_difference, set_intersection


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
  vector<char> vectorObject1, vectorObject2, v_res(26), vectorObject3;
  vector<char>::iterator p, pres_end;
  int i;

  for(i = 0; i <20; i++) 
      vectorObject1.push_back('A'+i);
  for(i=10; i <26; i++) 
      vectorObject2.push_back('A'+i);

  cout << "Contents of vectorObject1:\n";
  for(i = 0; i <vectorObject1.size(); i++)
    cout << vectorObject1[ i ];
  cout << endl;

  cout << "Contents of vectorObject2:\n";
  for(i = 0; i <vectorObject2.size(); i++)
    cout << vectorObject2[ i ];
  cout << endl;

  pres_end = set_union(vectorObject1.begin(), vectorObject1.end(), vectorObject2.begin(), vectorObject2.end(), v_res.begin());

  cout << "Set union: ";
  for(p=v_res.begin(); p!=pres_end; p++)
    cout << *p;
  cout << endl;

  pres_end = set_difference(vectorObject1.begin(), vectorObject1.end(), vectorObject2.begin(), vectorObject2.end(), v_res.begin());

  cout << "Set difference: ";
  for(p=v_res.begin(); p!=pres_end; p++)
    cout << *p;
  cout << endl;

  pres_end = set_symmetric_difference(vectorObject1.begin(), vectorObject1.end(), vectorObject2.begin(), vectorObject2.end(), v_res.begin());

  cout << "Set symmetric difference: ";
  for(p=v_res.begin(); p!=pres_end; p++)
    cout << *p;
  cout << endl;

  pres_end = set_intersection(vectorObject1.begin(), vectorObject1.end(), vectorObject2.begin(), vectorObject2.end(), v_res.begin());

  cout << "Set intersection: ";
  for(p=v_res.begin(); p!=pres_end; p++)
    cout << *p;
  cout << endl;

  // includes 
  vectorObject3.push_back('A');
  vectorObject3.push_back('C');
  vectorObject3.push_back('D');

  if(includes(vectorObject1.begin(), vectorObject1.end(), vectorObject3.begin(), vectorObject3.end()))
      cout << "vectorObject1 includes all of vectorObject3.";
  else
      cout << "vectorObject3 contains elements not found in vectorObject1.";

  return 0;
}

           
       








Related examples in the same category

1.Demonstrate inner_product() in vectorDemonstrate inner_product() in vector
2.Demonstrate partial_sum() in vectorDemonstrate partial_sum() in vector
3.Use a unary function object to determine even/odd.Use a unary function object to determine even/odd.
4.Demonstrate count_if and not1 in vectorDemonstrate count_if and not1 in vector
5.Another way to sort a sequence into descending order.Another way to sort a sequence into descending order.
6.Use a function adaptor in vectorUse a function adaptor in vector
7.Demonstrate insert_iterator in a vectorDemonstrate insert_iterator in a vector
8.Insert one vector into another using an insert iteratorInsert one vector into another using an insert iterator
9.Demonstrate back_insert_iterator in vectorDemonstrate back_insert_iterator in vector
10.Demonstrate front_insert_iterator in vectorDemonstrate front_insert_iterator in vector
11.Find minimum and maximum inside a vectorFind minimum and maximum inside a vector
12.Use for_each() in vectorUse for_each() in vector
13.Use the logical_not unary function object in vectorUse the logical_not unary function object in vector
14.Sorting a vector into descending order in vectorSorting a vector into descending order in vector
15.Using copy() in VectorUsing copy() in Vector
16.Using swap_ranges() in VectorUsing swap_ranges() in Vector
17.Exchange elements from two different types of containers.Exchange elements from two different types of containers.
18.Demonstrate find() and find_if() in vectorDemonstrate find() and find_if() in vector
19.Demonstrate search() in vectorDemonstrate search() in vector
20.Demonstrate mismatch() in vectorDemonstrate mismatch() in vector
21.Demonstrating sort() in vectorDemonstrating sort() in vector
22.Demonstrating partial_sort() in vectorDemonstrating partial_sort() in vector
23.Demonstrate binary_search() in vectorDemonstrate binary_search() in vector
24.Demonstrate lower_bound() in vectorDemonstrate lower_bound() in vector
25.Demonstrating remove() and replace() in vectorDemonstrating remove() and replace() in vector
26.Demonstrating unique() in vectorDemonstrating unique() in vector
27.Transforming a sequence in vectorTransforming a sequence in vector
28.Generating a sequence in vectorGenerating a sequence in vector
29.Rotate a sequence in vectorRotate a sequence in vector
30.Right-rotate a sequence in vectorRight-rotate a sequence in vector
31.Demonstrate random_shuffle() in vectorDemonstrate random_shuffle() in vector
32.Merge two sequences Merge two sequences
33.A vector may allocate more memory than it currently needs. A vector may allocate more memory than it currently needs.