fork(3) download
  1. #include <cstddef>
  2. #include <iostream>
  3. #include <iterator>
  4. #include <algorithm>
  5.  
  6. template<typename T>
  7. class my_array{
  8. T* data_;
  9. std::size_t size_;
  10.  
  11. public:
  12.  
  13. // ---------------------------------
  14. // Forward declaration
  15. // ---------------------------------
  16. class const_iterator;
  17.  
  18. // ---------------------------------
  19. // iterator class
  20. // ---------------------------------
  21. class iterator: public std::iterator<std::random_access_iterator_tag, T>
  22. {
  23. public:
  24. iterator(): p_(NULL) {}
  25. iterator(T* p): p_(p) {}
  26. iterator(const iterator& other): p_(other.p_) {}
  27. const iterator& operator=(const iterator& other) {p_ = other.p_; return other;}
  28.  
  29. iterator& operator++() {p_++; return *this;} // prefix++
  30. iterator operator++(int) {iterator tmp(*this); ++(*this); return tmp;} // postfix++
  31. iterator& operator--() {p_--; return *this;} // prefix--
  32. iterator operator--(int) {iterator tmp(*this); --(*this); return tmp;} // postfix--
  33.  
  34. void operator+=(const std::size_t& n) {p_ += n;}
  35. void operator+=(const iterator& other) {p_ += other.p_;}
  36. iterator operator+ (const std::size_t& n) {iterator tmp(*this); tmp += n; return tmp;}
  37. iterator operator+ (const iterator& other) {iterator tmp(*this); tmp += other; return tmp;}
  38.  
  39. void operator-=(const std::size_t& n) {p_ -= n;}
  40. void operator-=(const iterator& other) {p_ -= other.p_;}
  41. iterator operator- (const std::size_t& n) {iterator tmp(*this); tmp -= n; return tmp;}
  42. std::size_t operator- (const iterator& other) {return p_ - other.p_;}
  43.  
  44. bool operator< (const iterator& other) {return (p_-other.p_)< 0;}
  45. bool operator<=(const iterator& other) {return (p_-other.p_)<=0;}
  46. bool operator> (const iterator& other) {return (p_-other.p_)> 0;}
  47. bool operator>=(const iterator& other) {return (p_-other.p_)>=0;}
  48. bool operator==(const iterator& other) {return p_ == other.p_; }
  49. bool operator!=(const iterator& other) {return p_ != other.p_; }
  50.  
  51. T& operator[](const int& n) {return *(p_+n);}
  52. T& operator*() {return *p_;}
  53. T* operator->(){return p_;}
  54.  
  55. private:
  56. T* p_;
  57.  
  58. friend class const_iterator;
  59. };
  60.  
  61. // ---------------------------------
  62. // const_iterator class
  63. // ---------------------------------
  64. class const_iterator: public std::iterator<std::random_access_iterator_tag, T>
  65. {
  66. public:
  67. const_iterator(): p_(NULL) {}
  68. const_iterator(const T* p): p_(p) {}
  69. const_iterator(const iterator& other): p_(other.p_) {}
  70. const_iterator(const const_iterator& other): p_(other.p_) {}
  71. const const_iterator& operator=(const const_iterator& other) {p_ = other.p_; return other;}
  72. const const_iterator& operator=(const iterator& other) {p_ = other.p_; return other;}
  73.  
  74. const_iterator& operator++() {p_++; return *this;} // prefix++
  75. const_iterator operator++(int) {const_iterator tmp(*this); ++(*this); return tmp;} // postfix++
  76. const_iterator& operator--() {p_--; return *this;} // prefix--
  77. const_iterator operator--(int) {const_iterator tmp(*this); --(*this); return tmp;} // postfix--
  78.  
  79. void operator+=(const std::size_t& n) {p_ += n;}
  80. void operator+=(const const_iterator& other) {p_ += other.p_;}
  81. const_iterator operator+ (const std::size_t& n) const {const_iterator tmp(*this); tmp += n; return tmp;}
  82. const_iterator operator+ (const const_iterator& other) const {const_iterator tmp(*this); tmp += other; return tmp;}
  83.  
  84. void operator-=(const std::size_t& n) {p_ -= n;}
  85. void operator-=(const const_iterator& other) {p_ -= other.p_;}
  86. const_iterator operator- (const std::size_t& n) const {const_iterator tmp(*this); tmp -= n; return tmp;}
  87. std::size_t operator- (const const_iterator& other) const {return p_ - other.p_;}
  88.  
  89. bool operator< (const const_iterator& other) const {return (p_-other.p_)< 0;}
  90. bool operator<=(const const_iterator& other) const {return (p_-other.p_)<=0;}
  91. bool operator> (const const_iterator& other) const {return (p_-other.p_)> 0;}
  92. bool operator>=(const const_iterator& other) const {return (p_-other.p_)>=0;}
  93. bool operator==(const const_iterator& other) const {return p_ == other.p_; }
  94. bool operator!=(const const_iterator& other) const {return p_ != other.p_; }
  95.  
  96. const T& operator[](const int& n) const {return *(p_+n);}
  97. const T& operator*() const {return *p_;}
  98. const T* operator->() const {return p_;}
  99.  
  100. private:
  101. const T* p_;
  102. };
  103.  
  104. my_array()
  105. : data_(NULL), size_(0)
  106. {}
  107. my_array(std::size_t size)
  108. : data_(new T[size]), size_(size)
  109. {}
  110. my_array(const my_array<T>& other){
  111. size_ = other.size_;
  112. data_ = new T[size_];
  113. for (std::size_t i = 0; i<size_; i++)
  114. data_[i] = other.data_[i];
  115. }
  116. my_array(const const_iterator& first, const const_iterator& last){
  117. size_ = last - first;
  118. data_ = new T[size_];
  119.  
  120. for (std::size_t i = 0; i<size_; i++)
  121. data_[i] = first[i];
  122. }
  123.  
  124. ~my_array(){
  125. delete [] data_;
  126. }
  127. const my_array<T>& operator=(const my_array<T>& other){
  128. size_ = other.size_;
  129. data_ = new T[size_];
  130. for (std::size_t i = 0; i<size_; i++)
  131. data_[i] = other.data_[i];
  132. return other;
  133. }
  134. const T& operator[](std::size_t idx) const {return data_[idx];}
  135. T& operator[](std::size_t& idx) {return data_[idx];}
  136. std::size_t size(){return size_;}
  137.  
  138. iterator begin(){ return iterator(data_); }
  139. iterator end() { return iterator(data_+size_); }
  140. const_iterator begin() const{ return const_iterator(data_); }
  141. const_iterator end() const { return const_iterator(data_+size_);}
  142. };
  143.  
  144. template<typename T>
  145. void print(T t) {
  146. std::cout << t << std::endl;
  147. }
  148.  
  149. int main(){
  150.  
  151. int list [] = {1, 3, 5, 2, 4, 3, 5, 10, 10};
  152. my_array<int> a(list, list+sizeof(list)/sizeof(int));
  153.  
  154. // works!
  155. for (my_array<int>::const_iterator it = a.begin(), end = a.end();
  156. it != end; ++it)
  157. std::cout << ' ' << *it;
  158. std::cout << std::endl;
  159.  
  160. // works!
  161. std::for_each(a.begin(), a.end(), print<int>);
  162. std::cout << std::endl;
  163.  
  164. // works!
  165. my_array<int> b(a.size());
  166. std::copy(a.begin(), a.end(), b.begin());
  167.  
  168. // works!
  169. my_array<int>::iterator end = std::remove(a.begin(), a.end(), 5);
  170. std::for_each(a.begin(), end, print<int>);
  171. std::cout << std::endl;
  172.  
  173. // works!
  174. std::random_shuffle(a.begin(), end);
  175. std::for_each(a.begin(), end, print<int>);
  176. std::cout << std::endl;
  177.  
  178. // works!
  179. std::cout << "Counts of 3 in array = " << std::count(a.begin(), end, 3) << std::endl << std::endl;
  180.  
  181. // works!
  182. std::sort(a.begin(), end);
  183. std::for_each(a.begin(), end, print<int>);
  184. std::cout << std::endl;
  185.  
  186. // works!
  187. if (!std::binary_search(a.begin(), a.end(), 5))
  188. std::cout << "Removed!" << std::endl;
  189.  
  190. return 0;
  191. }
  192.  
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
 1 3 5 2 4 3 5 10 10
1
3
5
2
4
3
5
10
10

1
3
2
4
3
10
10

3
4
3
10
1
10
2

Counts of 3 in array = 2

1
2
3
3
4
10
10

Removed!