Help with Operator Overloading

This is a discussion on Help with Operator Overloading within the C++ Programming forums, part of the General Programming Boards category; This is a Set ADT and I am having problems with overloading the + operator to do a union of ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    55

    Help with Operator Overloading

    This is a Set ADT and I am having problems with overloading the + operator to do a union of two sets. What I was trying to do was load two vectors of integers to a temporary vector (removing the duplicates) but I have been unable to get it to work. Any suggestions please? Thanks.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <vector>
    
    using namespace std ;
    
    class Set 
    {
       public:
        void add (int anItem) ;
        void remove (int anItem) ;
        int size () ;
        
        bool is_empty () ;
        bool has (int& the_target_item) ;
       
        
        friend ostream& operator << (ostream& OUTPUT, Set& aSet) ;
        friend istream& operator >> (istream& INPUT, Set& aSet) ;
        friend Set& operator + (Set& aSet, Set& anotherSet) ;   
       
       private:
        vector <int> theItems ;
    
    };
    
    int main ()
    {
    
        
    system ("Pause") ;
    return 0 ;
    }
    
    
    void Set :: add (int anItem) 
    {
     if (Set :: has(anItem))
     {
      //no-op
     }
     else
     {
      theItems . push_back (anItem) ;
     }  
    }
    
    void Set :: remove (int anItem) 
    {
     for (int i = Set :: has (anItem); i < theItems . size () - 1; i++)
      {
       theItems[i] = theItems[i+1] ;
      }
     
     theItems . pop_back () ;
    }
    
    ostream& operator << (ostream& OUTPUT, Set& aSet) 
    {
     OUTPUT << "Current Size: " <<  aSet . theItems . size() << "  " << endl ; 
     for (int i=0; i < aSet . theItems . size(); i++) 
     {
      OUTPUT << aSet . theItems[i] << " " ;
     }
      OUTPUT << endl ;
      return OUTPUT ;
    }
    
    istream& operator >> (istream& INPUT, Set& aSet) 
    {
     int anItem ;
     cout << "Enter Items (Ctrl-z to End): " ;
     while ( cin >> anItem)
     {  
      aSet . add (anItem) ;
     }
     return INPUT ;
    }
    
    int Set :: size () 
    {
     return theItems . size () ;
    }
    
    Set operator + (Set aSet, Set anotherSet) 
    {
     vector <int> tmpVector ;
     tmpVector . push_back (aSet) ;
     tmpVector . push_back (anotherSet) ;
    }
    
    bool Set :: is_empty () 
    {
     if (theItems . size () == 0)
     {
      return true ;
     }    
     else
     {
      return false ;
     }
    }
    
    bool Set :: has (int& the_target_item) 
    {
     int i = 0 ;
     while ( i < theItems . size () && theItems [i] != the_target_item)
     {
      i++ ;
     }
     
     if ( i == theItems.size ( )  ) 	
     {		
      return false ;	
     }	
     else	
     {		
      return true ;	
     }   
    }

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,487
    Add some sort of index function that takes an int x and returns an int representing theItems[x]. Or overload operator [].

    Then you need something like this (assuming you created a function called index):

    Code:
    for (each element in aSet) {
        if ( find( tmpVector.begin(), tmpVector.end(), aSet.index(i) ) == tmpVector.end() ) {
            add aSet(index) to tmpVector;
        }
    }
    repeat for anotherSet
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    If you keep the vectors sorted (or switch to std::set<int>) you can use std::set_union() to merge them.

    There is also std::set_intersection() and std::set_difference().

    For example this would be your operator+ :

    Code:
    Set Set::operator+(const Set& other) const{
    
    	Set r;
    	std::set_union(theItems.begin(),theItems.end(),other.theItems.begin(),other.theItems.end(),
    				std::inserter(r.theItems,r.theItems.begin()));
    
    	return r;
    }
    Last edited by Cat; 11-29-2006 at 11:29 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21