fork download
  1. #include <iostream>
  2. #include <set>
  3. #include <string>
  4. #include <unordered_set>
  5. #include <vector>
  6.  
  7. namespace util
  8. {
  9. template<typename T, typename... R>
  10. struct ordered_set : private std::vector<T, R...>
  11. {
  12. private:
  13. using base = std::vector<T, R...>;
  14. using set = std::set<T, R...>;
  15. public:
  16. using typename base::value_type;
  17. using typename base::allocator_type;
  18. using typename base::size_type;
  19. using typename base::difference_type;
  20. using typename base::reference;
  21. using typename base::const_reference;
  22. using typename base::pointer;
  23. using typename base::const_pointer;
  24. using typename base::iterator;
  25. using typename base::const_iterator;
  26. using typename base::reverse_iterator;
  27. using typename base::const_reverse_iterator;
  28.  
  29. using base::get_allocator;
  30. using base::at;
  31. using base::operator[];
  32. using base::front;
  33. using base::back;
  34. using base::data;
  35. using base::begin;
  36. using base::cbegin;
  37. using base::end;
  38. using base::cend;
  39. using base::rbegin;
  40. using base::crbegin;
  41. using base::rend;
  42. using base::crend;
  43. using base::empty;
  44. using base::size;
  45.  
  46. explicit ordered_set(typename base::allocator_type const &alloc = typename base::allocator_type())
  47. : base(alloc)
  48. {
  49. }
  50. template<typename Iter>
  51. ordered_set(Iter first, Iter last, typename base::allocator_type const &alloc = typename base::allocator_type())
  52. : base(alloc)
  53. {
  54. for(set s; first != last; ++first)
  55. {
  56. if(s.insert(*first).second)
  57. {
  58. base::push_back(*first);
  59. }
  60. }
  61. }
  62. ordered_set(std::initializer_list<T> init, typename base::allocator_type const &alloc = typename base::allocator_type())
  63. : ordered_set(init.begin(), init.end(), alloc)
  64. {
  65. }
  66.  
  67. friend bool operator==(ordered_set const &a, ordered_set const &b){ return static_cast<base>(a) == static_cast<base>(b); }
  68. friend bool operator!=(ordered_set const &a, ordered_set const &b){ return static_cast<base>(a) != static_cast<base>(b); }
  69. friend bool operator< (ordered_set const &a, ordered_set const &b){ return static_cast<base>(a) < static_cast<base>(b); }
  70. friend bool operator<=(ordered_set const &a, ordered_set const &b){ return static_cast<base>(a) <= static_cast<base>(b); }
  71. friend bool operator> (ordered_set const &a, ordered_set const &b){ return static_cast<base>(a) > static_cast<base>(b); }
  72. friend bool operator>=(ordered_set const &a, ordered_set const &b){ return static_cast<base>(a) >= static_cast<base>(b); }
  73. };
  74. }
  75.  
  76. int main()
  77. {
  78. std::set<std::string> a1 {"a", "b"};
  79. std::set<std::string> b1 {"b", "a"};
  80. std::cout << std::boolalpha << (a1 == b1) << std::endl;
  81.  
  82. std::unordered_set<std::string> a2 {"a", "b"};
  83. std::unordered_set<std::string> b2 {"b", "a"};
  84. std::cout << std::boolalpha << (a2 == b2) << std::endl;
  85.  
  86. util::ordered_set<std::string> a3 {"a", "b"};
  87. util::ordered_set<std::string> b3 {"b", "a"};
  88. std::cout << std::boolalpha << (a3 == b3) << std::endl;
  89. }
  90.  
Success #stdin #stdout 0s 3436KB
stdin
Standard input is empty
stdout
true
true
false