fork download
  1. #include <vector>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. template <bool flag, class IsTrue, class IsFalse>
  6. struct choose;
  7.  
  8. template <class IsTrue, class IsFalse>
  9. struct choose<true, IsTrue, IsFalse> {
  10. typedef IsTrue type;
  11. };
  12.  
  13. template <class IsTrue, class IsFalse>
  14. struct choose<false, IsTrue, IsFalse> {
  15. typedef IsFalse type;
  16. };
  17.  
  18. class DefaultOrder {
  19. public:
  20. bool operator()(int i1,int i2) {return i1 < i2;}
  21. };
  22.  
  23. template<typename K, typename V, typename Order=DefaultOrder>
  24. struct multimap {
  25. struct pair {
  26. pair(K key, V value):first(key),second(value) {}
  27. K first;
  28. V second;
  29. bool operator<(pair other) {
  30. Order order;
  31. return order(first,other.first);
  32. }
  33. };
  34. template<bool isconst = false>
  35. struct Iterator {
  36. typedef typename choose<isconst, const pair&, pair&>::type
  37. reference;
  38. typedef typename choose<isconst, const pair*, pair*>::type
  39. pointer;
  40.  
  41. typedef typename vector<pair>::iterator it;
  42. struct ActualIterator {
  43. it i;
  44. vector<pair>& pairs;
  45. ActualIterator(vector<pair>& pairs,it i):pairs(pairs),i(i) {}
  46. bool operator!=(ActualIterator other) {
  47. return i != other.i;
  48. }
  49. pair& operator*() {
  50. auto& bla = *i;
  51. return bla;
  52. }
  53. virtual ActualIterator& operator++() = 0;
  54. };
  55. struct OneKeyIterator : public ActualIterator {
  56. K key;
  57. OneKeyIterator(vector<pair>& pairs, K key):ActualIterator(pairs,pairs.end()),key(key) {
  58. auto end = pairs.end();
  59. for(auto i_ = pairs.begin(); i_ != end; ++i_)
  60. if(i_->first == key) {
  61. i = i_;
  62. return;
  63. }
  64. }
  65. ActualIterator& operator++() {
  66. ++i;
  67. if(i != pairs.end() && i->first != key)
  68. i = pairs.end();
  69. return *this;
  70. }
  71. };
  72. struct WholeMapIterator : public ActualIterator {
  73. WholeMapIterator(vector<pair>& pairs, it i):ActualIterator(pairs,i){}
  74. ActualIterator& operator++() {
  75. ++i;
  76. return *this;
  77. }
  78. };
  79.  
  80. ActualIterator* actualIterator;
  81.  
  82. Iterator(vector<pair>& pairs, it i):actualIterator(new WholeMapIterator(pairs,i)) {
  83.  
  84. }
  85. Iterator(vector<pair>& pairs, K key):actualIterator(new OneKeyIterator(pairs,key)) {
  86. }
  87. Iterator(const Iterator<false>& i):actualIterator(i.actualIterator){}
  88. pointer operator->() {
  89. return &**actualIterator;
  90. }
  91. reference operator*() {
  92. return *actualIterator;
  93. }
  94. Iterator& operator++() {
  95. ++(*actualIterator);
  96. return *this;
  97. }
  98. bool operator!=(const Iterator& other) const {
  99. return *actualIterator != *other.actualIterator;
  100. }
  101. bool operator==(const Iterator& other) const {
  102. return !(*this != other);
  103. }
  104. };
  105.  
  106. typedef Iterator<false> MutableIterator;
  107. typedef Iterator<true> ConstIterator;
  108.  
  109. vector<pair> pairs;
  110. void insert(K key, V value) {
  111. pairs.push_back(pair(key,value));
  112. sort(pairs.begin(), pairs.end());
  113. }
  114. MutableIterator find(K key) {
  115. //auto end = pairs.end();
  116. //for(auto i = pairs.begin(); i != end; ++i)
  117. //auto p = *i;
  118. // if(i->first == key) {
  119. return MutableIterator(pairs,key);
  120. // }
  121. }
  122. MutableIterator begin() {
  123. return MutableIterator(pairs,pairs.begin());
  124. }
  125. MutableIterator end() {
  126. return MutableIterator(pairs,pairs.end());
  127. }
  128. };
  129.  
  130. int main()
  131. {
  132. multimap<int,int> mm;
  133. }
  134.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:46:18: error: cannot declare parameter 'other' to be of abstract type 'multimap<K, V, Order>::Iterator<isconst>::ActualIterator'
prog.cpp:42:31: note:   because the following virtual functions are pure within 'multimap<K, V, Order>::Iterator<isconst>::ActualIterator':
prog.cpp:53:37: note: 	virtual multimap<K, V, Order>::Iterator<isconst>::ActualIterator& multimap<K, V, Order>::Iterator<isconst>::ActualIterator::operator++()
prog.cpp: In constructor 'multimap<K, V, Order>::Iterator<isconst>::OneKeyIterator::OneKeyIterator(std::vector<multimap<K, V, Order>::pair>&, K)':
prog.cpp:61:25: error: 'i' was not declared in this scope
prog.cpp: In member function 'multimap<K, V, Order>::Iterator<isconst>::ActualIterator& multimap<K, V, Order>::Iterator<isconst>::OneKeyIterator::operator++()':
prog.cpp:66:19: error: 'i' was not declared in this scope
prog.cpp: In member function 'multimap<K, V, Order>::Iterator<isconst>::ActualIterator& multimap<K, V, Order>::Iterator<isconst>::WholeMapIterator::operator++()':
prog.cpp:75:19: error: 'i' was not declared in this scope
stdout
Standard output is empty