fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <type_traits>
  4. using namespace std;
  5.  
  6. // Stub
  7. struct Tree_ {};
  8.  
  9. template<typename Key, typename mapped_type>
  10. struct mp {
  11. class const_iterator
  12. {
  13. public:
  14. typedef const_iterator self_type;
  15. // HTRD: тут const как раз не нужен
  16. typedef /*const*/ std::pair<Key, mapped_type> value_type;
  17. typedef const value_type& reference;
  18. typedef const value_type* pointer;
  19. typedef std::forward_iterator_tag iterator_category;
  20. typedef int difference_type;
  21. //constructors
  22. const_iterator()
  23. {
  24. curr = nullptr;
  25. currPair = new value_type;
  26. printf("111\n");
  27. }
  28. const_iterator(Tree_ &n)
  29. {
  30. #if 0
  31. printf("2");
  32. curr = &n;
  33. currPair = new value_type;
  34. *currPair = std::make_pair(curr->key, curr->getValue());
  35. #endif
  36. }
  37. const_iterator(const self_type& other)
  38. {
  39. #if 0
  40. curr = other.curr;
  41. currPair = new value_type;
  42. *currPair = *other.currPair;
  43. #endif
  44. }
  45. const_iterator(const std::pair<Key, mapped_type>& other)
  46. {
  47. #if 0
  48. curr = other.curr;
  49. currPair = other.currPair;
  50. #endif
  51. }
  52. //operators
  53. //prefix increment
  54. self_type& operator++() {
  55. #if 0
  56. //сравни старое значение и новое
  57. if (currPair->second != curr->getValue())
  58. curr->setValue(currPair->second);
  59. curr = successor(); // point to next node
  60. if (curr)
  61. *currPair = std::make_pair(curr->key, curr->getValue());
  62. #endif
  63. return *this;
  64. }
  65. // postfix increment (it++)
  66. self_type operator++(int)
  67. {
  68. self_type old = *this;
  69. ++(*this);
  70. return old;
  71. }
  72. //reference
  73. // HTRD: тут const не нужен уже, т.к. к типам добавили
  74. reference operator*() const {
  75. return *currPair;
  76. }
  77. //pointer
  78. // HTRD: тут const не нужен уже, т.к. к типам добавили
  79. const pointer operator->() const // HTRD: тут забыли добавить const
  80. {
  81. return currPair;
  82. }
  83. // Inequality test operator
  84. bool operator!=(self_type const & other) const {
  85. return this->curr != other.curr;
  86. }
  87. // Dereference operator
  88. bool operator==(self_type const & other) const {
  89. return this->curr == other.curr;
  90. }
  91.  
  92. // HTRD: а это так надо? вы const_iterator готовы кастовать к iterator?
  93. //operator iterator() const { return iterator(); }
  94. private:
  95. pointer currPair;
  96. Tree_* curr;
  97. //Successor
  98. Tree_* successor()
  99. {
  100. #if 0
  101. if (curr)
  102. if (curr->right != 0) {
  103. curr = curr->right;
  104. while (curr->left != 0)
  105. curr = curr->left;
  106. }
  107. else {
  108. Tree_* y = curr->parent;
  109. if (y == nullptr)
  110. return nullptr;
  111. while (curr == y->right) {
  112. curr = y;
  113. y = y->parent;
  114. if (y == nullptr)
  115. return nullptr;
  116. }
  117. if (curr->right != y)
  118. curr = y;
  119. }
  120. return curr;
  121. #endif
  122. }
  123. }; // iterator
  124. }; // mp
  125.  
  126. int main()
  127. {
  128. using MapIt = mp<int,int>::const_iterator;
  129. MapIt tmp1;
  130. static_assert(std::is_const<typename std::remove_reference<decltype(*tmp1)>::type>::value, "returned value by dereferencing iterator is not const");
  131.  
  132. // your code goes here
  133. return 0;
  134. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
111