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
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_ */
Rashakil Fol 978 Super Senior Demiposter Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.