Thread: my vector class..easy dynamic arrays

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196

    Talking my vector class..easy dynamic arrays

    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;
    }
    Last edited by nextus; 02-01-2003 at 05:40 PM.
    nextus, the samurai warrior

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct types and dynamic arrays
    By simone.marras in forum C Programming
    Replies: 6
    Last Post: 03-14-2009, 10:56 AM
  2. Dynamic Arrays?
    By screenfreak in forum C Programming
    Replies: 2
    Last Post: 10-30-2006, 11:38 AM
  3. Creating New Dynamic Arrays
    By mas0nite in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2006, 02:59 PM
  4. Dynamic two dimensional arrays
    By ThWolf in forum C++ Programming
    Replies: 14
    Last Post: 08-30-2006, 02:28 PM
  5. 2D arrays with functions made easy
    By goran in forum C Programming
    Replies: 1
    Last Post: 09-17-2001, 12:08 PM