Thread: need help with iterators in a tree

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    13

    need help with iterators in a tree

    Having trouble with using iterators in a tree

    I'm having trouble with implementing code to iterator asset contrainer and sum by type ....commented in the code where it's to go but also need to implement a loop to scan m_Borrowers container for borrower by phone number. Both are commented out where they are to go. Can you help...almost through this book...stuck on this one.



    Code:
    //hierarchial data model ==> a general tree example
    //
    //root (first level) - Loan
    //second level - Loan can have zero to 'n' Borrowers
    //third level - Borrower can have zero to 'n' Assets
    
    #pragma warning (disable : 4786)
    
    #include <iostream>
    #include <list>
    #include <string>
    
    using namespace std;
    
    ///////////////////////////////////////////////////////////////////////////////////////////
    
    class Asset
    {
    public:
    	enum eAssetType
    	{
    		eAllAssets,
    		eAssetChecking,
    		eAssetSavings,
    		eAssetRealEstate,
    		eAssetStock,
    		eAssetBond,
    		eAssetMutualFund,
    		eAssetLifeIns,
    		eAsset401K,
    		eAssetIRA,
    	};
    
    
    	Asset (eAssetType a_type, char *a_sinstname, double a_value)
    	{
    		m_eType			= a_type;
    		m_sInstitution	= a_sinstname;
    		m_Value			= a_value;
    	};
    
    	~Asset (void) {};
    
    	eAssetType  m_eType;
    	string      m_sInstitution;
    	double      m_Value;
    };
    
    ///////////////////////////////////////////////////////////////////////////////////////////////
    
    class Borrower
    {
    public:
    	Borrower (void) {};
    	~Borrower (void) {};
    
    	Asset* AddAsset (Asset::eAssetType a_type, char *a_sinstname, double a_value)
    	{
    		Asset *pa = new Asset (a_type, a_sinstname, a_value);
    
    		m_Assets.push_back (pa);
    
    		return (pa);
    	};
    
    	double SumAssets (Asset::eAssetType a_type)
    	{
    		double sum_of_assets = 0.0;
    
    		Asset *pa = 0;
    
    
    //implement code to iterate asset container and sum by type here
    		list <Borrower*> stlList;
    		typedef list<Borrower*>::iterator AssetIterator;
    		sum_of_assets = sum_of_assets + Borrower*
    
    		return (sum_of_assets);
    	};
    
    	string m_sFirstName;
    	string m_sLastName;
    	string m_sPhoneNumber;
    	string m_sSSN;
    
    private:
    	list <Asset*> m_Assets;
    };
    
    ////////////////////////////////////////////////////////////////////////////////////
    
    class Loan
    {
    public:
    	Loan (void) { };
    	~Loan  (void) { };
    
    	Borrower* AddBorrower (char *a_sfirstname, char *a_slastname, char *a_sphonenumber, char *a_sssn)
    	{
    		Borrower *pb = new Borrower;
    
    		pb->m_sFirstName	= a_sfirstname;
    		pb->m_sLastName		= a_slastname;
    		pb->m_sPhoneNumber	= a_sphonenumber;
    		pb->m_sSSN			= a_sssn;
    
    		m_Borrowers.push_back (pb);
    
    		return pb;
    	};
    
    	Borrower* GetBorrower (const char *a_sphonenumber)
    	{
    		Borrower *pb=0;
    
    //implement for loop to scan m_Borrowers container for borrower by phone number here
    
    		return (pb);
    	}
    
    private:
    	list <Borrower*> m_Borrowers;
    };
    
    ////////////////////////////////////////////////////////////////////////////////////
    
    void main (void)
    {
    	Loan loan;  //declare an instance of our root node
    
    	Borrower *pb1 = loan.AddBorrower ("Ralph", "Pratt", "319-263-0372", "123-45-6789");
    	Borrower *pb2 = loan.AddBorrower ("Sally", "Pratt", "319-264-8901", "987-65-4321");
    
    	pb1->AddAsset (Asset::eAssetChecking,		"Wells Fargo",	2495.00);
    	pb1->AddAsset (Asset::eAsset401K,			"Employer",	   12459.00);
    	pb1->AddAsset (Asset::eAssetMutualFund,		"Fidelity",	   23000.00);
    
    	cout << "Borrower named: " << pb1->m_sFirstName << " " << pb1->m_sLastName << " sum of all assets: "
    		<< pb1->SumAssets(Asset::eAllAssets) << endl;
    	cout << "Borrower named: " << pb2->m_sFirstName << " " << pb2->m_sLastName << " sum of all assets: "
    		<< pb2->SumAssets(Asset::eAllAssets) << endl;
    
    	string starget = "319-263-2730";
    
    	Borrower *pb = loan.GetBorrower (starget.c_str());
    
    	if (0 == pb )
    
    		cout << "Cannot find borrower with phone number: " << starget << endl;
    	else
    		cout << "found phone number: " << starget << endl;
    
    	starget = "319-263-0372";
    
    	pb = loan.GetBorrower (starget.c_str());
    
    	if ( 0 == pb )
    		cout << "cannot find borrower with phone number: " << starget << endl;
    	else
    		cout << "found borrower phone number: " << starget << " " << pb->m_sFirstName << " " << pb->
    			m_sLastName << endl;
    }
    tinkerbelle

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Simple container iteration:
    Code:
    #include <list>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        list<int> mylist;
    
        for (int n = 0; n <= 10; n++)
            mylist.push_back(n);
    
        int sum = 0;
        list<int>::iterator it;
        for (it = mylist.begin(); it != mylist.end(); it++)
        {
            cout << *it << endl;
    
            sum += *it;
        }//for
    
        cout << "---------" << endl;
        cout << sum << endl;
    
        return 0;
    }//main
    gg

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    13

    Angry Still need help with this

    CodePlug, I'm still having problems understanding how to use this with pointers as well.

    Can you point (no pun intended) me in the right direction?

    Thanks

    Tinkerbelle
    tinkerbelle

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Not much different. You just need to understand the difference between "*it" and "it->".
    Code:
    #include <list>
    #include <iostream>
    using namespace std;
    
    class Integer
    {
    public:
        int m_value;
        Integer(int val) : m_value(val) {}
    };//Integer
    
    int main()
    {
        list<Integer*> mylist;
    
        for (int n = 0; n <= 10; n++)
            mylist.push_back(new Integer(n));
    
        int sum = 0;
        list<Integer*>::iterator it;
        for (it = mylist.begin(); it != mylist.end(); it++)
        {
            // "it->" will return type "Integer**", which isn't usefull in this case
            // "*it" will return type "Integer*", the templated type
            int val = (*it)->m_value;
    
            cout << val << endl;
    
            sum += val;
        }//for
    
        cout << "---------" << endl;
        cout << sum << endl;
    
        // don't leak memory, that would be shameful
        for (it = mylist.begin(); it != mylist.end(); it++)
            delete *it;
    
        return 0;
    }//main
    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM