Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- template <typename T>
- class Vector{
- T* ptr;
- int size;
- int curr;
- public:
- Vector(){
- this->size = 2;
- this->ptr = new T[this->size];
- this->curr = 0;
- }
- ~Vector(){
- delete [] this->ptr;
- }
- void add(T &t){
- ++this->curr;
- if (this->curr>this->size){
- this->size=this->size+100;
- T* tmp = new T[this->size];
- for (int i = 0; i < this->curr; ++i) {
- tmp[i]=this->ptr[i];
- }
- delete this->ptr;
- this->ptr=tmp;
- }
- this->ptr[curr]=t;
- }
- Vector(const Vector &prev) {
- this->size=prev.size;
- this->curr=prev.curr;
- this->ptr = new T[this->size];
- for (int i = 0; i < this->curr; ++i) {
- this->ptr[i]=prev.ptr[i];
- std::cout << this->ptr[i]<<", ";
- }
- std::cout << std::endl<<"copy ended"<<std::endl;
- }
- };
- int main() {
- Vector <int>vec;
- Vector <int>ve(vec);
- int n = 2;
- vec.add(n);
- vec.add(n);
- vec.add(n);
- vec.add(n);
- vec.add(n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement