NickAndNick

Реализация итераторов

Jul 7th, 2019
125
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <iterator>
  3. #include <random>
  4. using namespace std;
  5. template<typename Type>
  6. class Box {
  7. public:
  8.     using iterator = Type*;
  9.     using const_iterator = const Type*;
  10.     Box() : limit_(0U), ptr_(nullptr) { }
  11.     explicit Box(const size_t limit) : limit_(limit) {
  12.         ptr_ = new Type[limit_];
  13.     }
  14.     Box(initializer_list<Type> lst) : ptr_(new Type[lst.size()]), limit_(lst.size()) {
  15.         copy(lst.begin(), lst.end(), this->begin());
  16.     }
  17.    
  18.     Box(Box& box) {
  19.         limit_= box.limit_;
  20.         ptr_ = new Type[limit_];
  21.         copy(box.begin(), box.end(), this->begin());
  22.     }
  23.     Box& operator=(Box& box) {
  24.         if (this != &box) {
  25.             if (ptr_) delete[] ptr_;
  26.             limit_ = box.limit_;
  27.             ptr_ = new Type[limit_];
  28.             copy(box.begin(), box.end(), this->begin());
  29.         }
  30.         return *this;
  31.     }
  32.     ~Box() {
  33.         if (ptr_) {
  34.             delete[] ptr_;
  35.             ptr_ = nullptr;
  36.         }
  37.     }
  38.     int& operator[](const int index) {
  39.         return ptr_[index];
  40.     }
  41.     const int& operator[](const int index)const {
  42.         return ptr_[index];
  43.     }
  44.     size_t size()const {
  45.         return limit_;
  46.     }
  47.     bool empty()const {
  48.         return limit_ == 0U;
  49.     }
  50.     iterator begin()const {
  51.         return ptr_;
  52.     }
  53.     iterator end()const {
  54.         return ptr_ + limit_;
  55.     }
  56.     const_iterator cbegin()const {
  57.         return ptr_;
  58.     }
  59.     const_iterator cend()const {
  60.         return ptr_ + limit_;
  61.     }
  62.     auto rbegin()const {
  63.         const reverse_iterator<iterator> ri(end());
  64.         return ri;
  65.     }
  66.     auto rend()const {
  67.         const reverse_iterator<iterator> ri(begin());
  68.         return ri;
  69.     }
  70.     auto crbegin()const {
  71.         const reverse_iterator<const_iterator> cri(rbegin());
  72.         return cri;
  73.     }
  74.     auto crend()const {
  75.         const reverse_iterator<const_iterator> cri(rend());
  76.         return cri;
  77.     }
  78. private:
  79.     size_t limit_;
  80.     Type* ptr_;
  81. };
  82. int main() {
  83.     Box<int> box = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  84.     for (const auto value : box) cout << value << ' ';
  85.     cout.put('\n');
  86.     for (auto iter = box.crbegin(); iter != box.crend(); ++iter) cout << *iter << ' ';
  87.     cout.put('\n');
  88.     auto other = box;
  89.     other[3] = 8;
  90.     copy(other.cbegin(), other.cend(), ostream_iterator<int>(cout, " "));
  91.     cout.put('\n');
  92.     for (auto i = 0U; i < size(box); ++i) cout << box[i] << ' ';
  93.     cout.put('\n');
  94.     const uniform_int_distribution<> uid(1, static_cast<int>(size(other)));
  95.     random_device rd;
  96.     mt19937 gen(rd());
  97.     generate(other.begin(), other.end(), [&]() { return uid(gen); });
  98.     copy(other.rbegin(), other.rend(), ostream_iterator<int>(cout, " "));
  99.     cout.put('\n');
  100.     Box<int> tmp;
  101.     tmp = other;
  102.     for (auto iter = tmp.crbegin(); iter != tmp.crend(); ++iter) cout << *iter << ' ';
  103.     cout.put('\n');
  104.     system("pause");
  105. }
RAW Paste Data