Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iterator>
- #include <random>
- using namespace std;
- template<typename Type>
- class Box {
- public:
- using iterator = Type*;
- using const_iterator = const Type*;
- Box() : limit_(0U), ptr_(nullptr) { }
- explicit Box(const size_t limit) : limit_(limit) {
- ptr_ = new Type[limit_];
- }
- Box(initializer_list<Type> lst) : ptr_(new Type[lst.size()]), limit_(lst.size()) {
- copy(lst.begin(), lst.end(), this->begin());
- }
- Box(Box& box) {
- limit_= box.limit_;
- ptr_ = new Type[limit_];
- copy(box.begin(), box.end(), this->begin());
- }
- Box& operator=(Box& box) {
- if (this != &box) {
- if (ptr_) delete[] ptr_;
- limit_ = box.limit_;
- ptr_ = new Type[limit_];
- copy(box.begin(), box.end(), this->begin());
- }
- return *this;
- }
- ~Box() {
- if (ptr_) {
- delete[] ptr_;
- ptr_ = nullptr;
- }
- }
- int& operator[](const int index) {
- return ptr_[index];
- }
- const int& operator[](const int index)const {
- return ptr_[index];
- }
- size_t size()const {
- return limit_;
- }
- bool empty()const {
- return limit_ == 0U;
- }
- iterator begin()const {
- return ptr_;
- }
- iterator end()const {
- return ptr_ + limit_;
- }
- const_iterator cbegin()const {
- return ptr_;
- }
- const_iterator cend()const {
- return ptr_ + limit_;
- }
- auto rbegin()const {
- const reverse_iterator<iterator> ri(end());
- return ri;
- }
- auto rend()const {
- const reverse_iterator<iterator> ri(begin());
- return ri;
- }
- auto crbegin()const {
- const reverse_iterator<const_iterator> cri(rbegin());
- return cri;
- }
- auto crend()const {
- const reverse_iterator<const_iterator> cri(rend());
- return cri;
- }
- private:
- size_t limit_;
- Type* ptr_;
- };
- int main() {
- Box<int> box = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- for (const auto value : box) cout << value << ' ';
- cout.put('\n');
- for (auto iter = box.crbegin(); iter != box.crend(); ++iter) cout << *iter << ' ';
- cout.put('\n');
- auto other = box;
- other[3] = 8;
- copy(other.cbegin(), other.cend(), ostream_iterator<int>(cout, " "));
- cout.put('\n');
- for (auto i = 0U; i < size(box); ++i) cout << box[i] << ' ';
- cout.put('\n');
- const uniform_int_distribution<> uid(1, static_cast<int>(size(other)));
- random_device rd;
- mt19937 gen(rd());
- generate(other.begin(), other.end(), [&]() { return uid(gen); });
- copy(other.rbegin(), other.rend(), ostream_iterator<int>(cout, " "));
- cout.put('\n');
- Box<int> tmp;
- tmp = other;
- for (auto iter = tmp.crbegin(); iter != tmp.crend(); ++iter) cout << *iter << ' ';
- cout.put('\n');
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement