Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- template <typename T>
- class Vector
- {
- private:
- T* _array;
- int _size;
- int capacity;
- public:
- Vector ()
- {
- _array = new T[1];
- _size = 0;
- capacity = 1;
- }
- void reserve(int new_capacity)
- {
- T* new_array = new T[new_capacity];
- for (int i = 0; i < _size; i++)
- {
- new_array[i] = _array[i];
- }
- capacity = new_capacity;
- delete [] _array;
- _array = new_array;
- }
- void push_back(T element)
- {
- if(_size == capacity)
- reserve(_size*2);
- _array[_size ++] = element;
- }
- void get_resize(int n)
- {
- if (capacity < n ) _size = n;
- else
- {
- int t;
- t = n;
- capacity = t*2;
- _size=n;
- }
- }
- T& operator[](int n)
- {
- return _array[n];
- }
- void insert(int n, T element)
- {
- if (_size == capacity) reserve(_size*2);
- for (int i = _size; i > n; i--)
- _array[i] = _array[i-1];
- _array[n] = element;
- }
- void clear()
- {
- delete []_array;
- _array = new T[1];
- _size = 0;
- capacity = 1;
- }
- void erase(int n)
- {
- for (int i = n; i < (_size-1); i++)
- {
- _array[i]=_array[i+1];
- }
- _size--;
- }
- void pop_back()
- {
- _size--;
- }
- void swap(int a, int b)
- {
- T temp = _array[a];
- _array[a] =_array[b];
- _array[b] = temp;
- }
- int getSize()
- {
- return _size;
- }
- int getCapacity()
- {
- return capacity;
- }
- bool empty()
- {
- return (_size == 0);
- }
- T& it_begin()
- {
- return _array[0];
- }
- T& it_end()
- {
- return _array[_size];
- }
- };
- int main()
- { setlocale(LC_CTYPE, "rus");
- Vector<int> vector_;
- cout<<"Size:";
- int _size;
- cin>>_size;
- cout<<"Elements:"<<endl;
- int element;
- for(int i=0;i< _size; i++)
- {
- cin>>element;
- vector_.push_back(element);
- }
- vector_.get_resize(8);
- for (int i =0;i<vector_.getSize();i++) {
- cout <<vector_[i];
- cout<<endl;
- }
- cout << "Size:" << vector_.getSize();
- /* cout << endl;
- cout << "Size:" << vector_.getSize() << " " << "Empty?" << vector_.empty() << endl;
- cout<<"Clear vector"<<endl;
- vector_.clear();
- cout<<"Empty?"<<vector_.empty()<<endl;
- cout<<"Add 1"<<endl;
- cout<<"Add 2"<<endl;
- cout<<"Add 3"<<endl;
- vector_.push_back(1);
- vector_.push_back(2);
- vector_.push_back(3);
- cout<<"Swap index 0 and index 2 "<<endl;
- vector_.swap(0,2);
- cout<<"Delete inlex 1"<<endl;
- vector_.erase(1);
- cout<<"Vector:"<<endl;
- for (int i =0;i<vector_.getSize();i++) {
- cout <<vector_[i];
- cout<<endl;
- }
- cout << vector_.it_begin();
- cout << vector_.it_end();
- */return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement