Thread: Help with defining an iterator class

  1. #1
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90

    Help with defining an iterator class

    I am trying to write my own iterator (BIter) class to handle my generic bag class. How do I define my .begin and end functions which are found in the bag class definition, not the iterator class?

    Code:
    template<class item>
    class BIter
    {
    	public:
    		BIter(){}
    		item& operator*() {return *ptr;}
    		BIter& operator++(){ptr++; return *this; }
    		bool operator==(const BIter& b)
    		{
    			return ptr==b.ptr;
    		}
    	private:
    		item *ptr;
    };
    
    template<class type>//allows more variability than typedef
    class bag
    {
    public:
    	bag(int init_cap=30);
    	bag(bag& b); //copy constructor
    	void insert(type);
    	void remove(type);
    	void operator=(bag b);
    	//Post: The bag has a deep copy of bag b
    	~bag();//destructor
    	typedef BIter<type> Iterator;
    	Iterator begin()
    	{
    		type* x=pData;
    		return x;
    	}
    	Iterator end()
    	{
    		type* x=pData[used];
    		return x;
    	}
    
    private:
    	int used;//a member to record the number of items in bag
    	type *pData;//a pointer to point to the dynamic array
    	int capacity;//a member to record the capacity of a bag
    };
    
    
    template<class type>
    bag<type>::bag(int init_cap)
    {
    	used=0;//an empty bag
    	pData=new type[init_cap];//a dynamic array is created
    	                         //and assigned to the pointer pData
    	capacity=init_cap;
    }
    
    template<class type>
    void bag<type>::insert(type x)
    {
    	if(used==capacity)
    		return;
    	pData[used]=x;
    	used++;
    }
    
    template<class type>
    void bag<type>::remove(type t)
    {
    	for(int i=0;i<used;i++)
    		if(pData[i]==t)
    		{
    			pData[i]=pData[used-1];
    			used--;
    			return;
    		}
    }
    
    template<class type>
    void bag<type>::operator=(bag b)
    {
    	if(capacity!=b.capacity)
    		return;
    
    	for(int i=0;i<b.used;i++)
    		pData[i]=b.pData[i];
    
    	used=b.used;
    }
    
    template<class type>
    bag<type>::bag(bag& b)
    {
    	capacity=b.capacity;
    	pData=new type[capacity];
    	for(int i=0;i<b.used;i++)
    		pData[i]=b.pData[i];
    	used=b.used;
    }
    
    template<class type>
    bag<type>::~bag()
    {
    	delete [] pData;
    }

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    Maintain a pointer to the first and last elements of your array within your bag class. Provide a constructor for you iterator class that accepts a pointer to a type. Construct an iterator from the pointers to the first and last elements and return them from your begin/end members.
    Joe

  3. #3
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    Thank you! You have helped me to no end, I really appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Defining const in a class
    By g4j31a5 in forum C++ Programming
    Replies: 5
    Last Post: 11-20-2006, 11:27 AM
  2. Defining matricies
    By rich999 in forum C++ Programming
    Replies: 6
    Last Post: 12-02-2005, 07:59 PM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM