Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- template<typename Type, size_t size_>
- class array{
- public:
- typedef Type* pointer;
- typedef Type& ref;
- typedef Type value_type;
- template<typename... U>
- array(U... x) : a{x...} {}
- ref operator[](size_t index){
- return a[index];
- }
- class iterator{
- public:
- iterator(pointer ptr_) : ptr(ptr_){}
- iterator(const iterator& other) = default;
- value_type operator[](size_t index) const{
- return ptr[index];
- }
- bool operator ==(const iterator& other){
- return(ptr == other.ptr);
- }
- bool operator !=(const iterator& other){
- return(ptr != other.ptr);
- }
- ref operator*() const{
- return *ptr;
- }
- pointer operator->() const{
- return ptr;
- }
- iterator& operator++(){
- ++ptr;
- return *this;
- }
- iterator operator++(int){
- iterator tmp = *this;
- ++(*this);
- return tmp;
- }
- iterator& operator--(){
- --ptr;
- return *this;
- }
- iterator operator--(int){
- iterator tmp = *this;
- --(*this);
- return tmp;
- }
- private:
- pointer ptr;
- };
- iterator begin(){
- return a;
- }
- iterator end(){
- return(&a[size_]);
- }
- private:
- value_type a[size_];
- };
- int main(){
- int a[3] = {1,2,3};
- array<int,5> arr = {1,2,3,4,5};
- for(auto it = arr.begin(); it != arr.end(); it++){
- std::cout << *it << " ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement