fork download
  1. #include <set>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <functional>
  5. using namespace std;
  6.  
  7. template < typename T >
  8. class MCmp: public std::binary_function<T,T,bool>
  9. {
  10. public:
  11. MCmp ( bool mode_ ) : mode ( mode_ )
  12. {}
  13. bool operator() ( const T & first , const T & second )
  14. {
  15. return mode?first<second:second<first ;
  16. }
  17. bool operator== ( const MCmp & cmp )
  18. {
  19. return mode == cmp.mode ;
  20. }
  21. private:
  22. const bool mode ;
  23. } ;
  24.  
  25.  
  26. typedef set<int, MCmp<int> > MSet ;
  27.  
  28.  
  29. MSet createMSet ( bool x )
  30. {
  31. MCmp<int> cmp(x) ;
  32. MSet a(cmp);
  33. const int n=10;
  34. for (int i = 0; i < n; i++)
  35. a.insert(rand() % 11);
  36. return a ;
  37. }
  38.  
  39. template < typename T , typename U >
  40. std::ostream & operator<< ( std::ostream & stream , const std::set<T,U> obj )
  41. {
  42. for ( typename std::set<T,U>:: iterator it = obj.begin() , itEnd = obj.end() ; it!=itEnd ; ++it )
  43. stream << *it << ' ' ;
  44. return stream ;
  45. }
  46.  
  47. int main()
  48. {
  49. int x1 = 0 , x2 = 0 ;
  50. std::cin >> x1 >> x2 ;
  51. MSet a ( createMSet( x1 ) ) ;
  52. MSet b ( createMSet( x2 ) ) ;
  53. std::cout << a << std::endl << b << std::endl ;
  54. }
  55.  
Success #stdin #stdout 0s 3480KB
stdin
1 0
stdout
0 1 2 3 4 6 10 
10 9 8 7 5 4 3 2 0