Use set_union to union two lists : union « Set Multiset « C++

Home
C++
1.Bitset
2.Class
3.Console
4.Data Structure
5.Data Type
6.Deque
7.Development
8.File
9.Function
10.Generic
11.Language
12.List
13.Map Multimap
14.Overload
15.Pointer
16.Qt
17.Queue Stack
18.Set Multiset
19.STL Algorithms Binary search
20.STL Algorithms Heap
21.STL Algorithms Helper
22.STL Algorithms Iterator
23.STL Algorithms Merge
24.STL Algorithms Min Max
25.STL Algorithms Modifying sequence operations
26.STL Algorithms Non modifying sequence operations
27.STL Algorithms Sorting
28.STL Basics
29.String
30.Valarray
31.Vector
C / ANSI-C
C Tutorial
C++ Tutorial
Visual C++ .NET
C++ » Set Multiset » unionScreenshots 
Use set_union to union two lists
  
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <string>
#include <vector>

using namespace std;

class PC
{
   public:
   enum part keyboard, mouse, monitor };

   PCpart a_part = PC::keyboard, int id = );
   bool operator<const PC& rhs const;

   void print() const;

   private:
   part part_;
   int id_;
};

inline
PC::PCpart a_part, int id : part_a_part ), id_id ){}

inline bool PC::operator<const PC& rhs const{  
   return id_ < rhs.id_; 
}

void PC::print() const {
   string component;
   ifpart_ == keyboard )
      component = "keyboard";
   else ifpart_ == mouse )
      component = "mouse";
   else
      component = "monitor";

   cout << "ID: " << setw<< left << id_ << " PC: " << component << endl;
}

int main( )
{
   list<PC> listA;
   listA.push_backPCPC::keyboard, ) );
   listA.push_backPCPC::mouse, ) );
   listA.push_backPCPC::monitor, ) );
   listA.push_backPCPC::keyboard, ) );
   listA.push_backPCPC::monitor, ) );

   list<PC> inspector_BlistA );
   inspector_B.front() = PCPC::mouse, );
   inspector_B.back() = PCPC::monitor, );

   // must sort before using set algorithms
   listA.sort();
   inspector_B.sort();

   for_eachlistA.begin(), listA.end(),mem_fun_ref&PC::print ) );
   for_eachinspector_B.begin(), inspector_B.end(),mem_fun_ref&PC::print ) );

   vector<PC> result;
   // make vector large enough to hold all inspected parts
   result.resizelistA.size() + inspector_B.size() );

   vector<PC>::iterator the_end = set_unionlistA.begin(), listA.end(),inspector_B.begin(), inspector_B.end(), result.begin() );
   for_eachresult.begin(), the_end, mem_fun_ref&PC::print ) );

}
  
    
  
Related examples in the same category
1.Union two sets
2.Create the union of two lists
3.disjoint() algorithm: if they contain no elements in common
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.