i was bored and wrote a vector class for arrays...i think its makes arrays easiers for people...took me around one hour to think of it up...please try it...feedback will be great..and please feel free to use it... you can download it or copy the source code..here is the source code
Code:
/**********************************
********** vector class ***********
********** by: nextus ***********
**** [email protected] ****
**********************************/
// beta 1, feel free to add to this class
template<class t>
class vector
{
public:
// constructors
vector();
vector(const vector<t>& aObject);
vector(int count);
vector(int count, t value);
// destructor
~vector();
// operators
t& operator[](int value) const;
void operator=(const vector<t> aObject);
// member functions
void resize(int value);
int size() const;
public://private:
t* m_array;
int m_size;
};
// empty array, size = 0
template<class t>
vector<t>::vector()
{
m_array = 0;
m_size = 0;
}
// copy constructor, copies another object of <class t>
template<class t>
vector<t>::vector(const vector<t>& aObject)
{
int count = aObject.m_size;
m_array = new t[count];
m_size = count;
for(int i = 0; i < count; i++)
{
*(m_array + i) = *(aObject.m_array + i);
}
}
// creates an array of size count
template<class t>
vector<t>::vector(int count)
{
m_array = new t[count];
m_size = count;
}
// creates an array of size count and initializes it to value
template<class t>
vector<t>::vector(int count, t value)
{
m_array = new t[count];
m_size = count;
for(int i = 0; i < count; i++)
{
*(m_array + i) = value;
}
}
// destroy the dynamic array
template<class t>
vector<t>::~vector()
{
delete [] m_array;
}
// regular array notation returns value in the index of m_array[value]
template<class t>
t& vector<t>::operator[](int value) const
{
return *(m_array + value);
}
// sets an object of vector to equal to object of another vector
// 1)if the Rvalue is smaller then the Lvalue will be set smaller
// and the Lvalues will be set to equal all the Rvalues
// 2)if the Rvalue is bigger then the Lvalue will be set bigger
// and the Lvalues will be set to equal all the Rvalues
template<class t>
void vector<t>::operator=(const vector<t> aObject)
{
int count = aObject.m_size;
delete [] m_array;
m_array = new t[count];
for(int i = 0; i < count; i++)
{
*(m_array + i) = *(aObject.m_array + i);
}
m_size = count;
}
// resizes the array and keeps all values if resizes is bigger than
// the array, if its smaller than it keeps the values up until
// the value of the variable value
template<class t>
void vector<t>::resize(int value)
{
t* tempArray = new t[value];
m_size = value;
for(int i = 0; i < m_size; i++)
{
*(tempArray + i) = *(m_array + i);
}
delete [] m_array;
m_array = new t[value];
for(i = 0; i < value; i++)
{
*(m_array + i) = *(tempArray + i);
}
delete [] tempArray;
}
// returns the size of the array
template<class t>
int vector<t>::size() const
{
return m_size;
}