0

Here's some simulation of the basic push_back and pop_back functions from stl vectors, "inspired" by this thread: http://www.daniweb.com/software-development/cpp/threads/427912/question-about-a-simple-vector-class-template

Start New Discussion within our **Software Development Community** # Simulation of functions from stl::vector

```
/*
* vect.h
* vect.h is licensed under GNU GENERAL PUBLIC LICENSE
* Created on: Jul 12, 2012
* Author: sin
*/
#ifndef VECT_H_
#define VECT_H_
#define _SIZE 0
template <class T>
class vector{
T* arr;
int _size;
int _capacity;
public:
vector(){
_size=_SIZE;
_capacity=1;
arr=new T[_capacity];
}
void resize(int size=-1){
if (size==-1) size=_capacity=2*_size;
if (size<=_size && size!=-1) return;
T* temp_array=new T[size];
for (int i=0;i<_size;i++){
temp_array[i]=arr[i];
}
_capacity=size;
arr=temp_array;
delete temp_array;
}
void push_back(const T& elem){
arr[_size]=elem;
_size++;
if (_size==_capacity) resize();
}
T pop_back(){
_size--;
T ret=arr[_size];
return (ret);
}
T operator[](int index){
return (arr[index]);
}
int size(){
return (_size);
}
~vector(){
delete arr;
}
};
#endif /* VECT_H_ */
```

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.