help me with some idea...

This is a discussion on help me with some idea... within the C++ Programming forums, part of the General Programming Boards category; hi everyone ... i am doing the intersection, can someone let me know which intersect with which please... as i ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    141

    help me with some idea...

    hi everyone ... i am doing the intersection, can someone let me know which intersect with which please... as i am really confuse on the StringSet otherset .. here is my code and class...

    which is on red color..

    this is my implementation code
    Code:
    #include "stringset.h"
    
    // insert a value into the set
    // Pre: a string value
    // Post: the value is inserted into the set if it 
    //      is not already present
    void StringSet::insert(string value) 
    {
         if( !contains(value) )
         {
             contents.push_back(value);
         }
    }     
    
    // test for a value
    // Pre: a string value
    // Post: returns 1 if the value is present in the StringSet, 0 otherwise
    int StringSet::contains(string value) 
    {
        int loc;
        bool found = false;
        
        for(loc = 0; loc < contents.size(); loc++)
             if(contents[loc] == value)
             {
                  found = true;
                  break;
             }
        if(found)
             return 1;
        else
             return 0;
      
    }
    // intersection of this set with another
    // Pre: a StringSet object
    // Post: returns a new StringSet which is the intersection 
    //       of this  with otherset, ie. only the items common to both StringSets
    StringSet StringSet::set_intersection(StringSet otherset)
    {
              
    
    }
    
    // union of this set with another
    // Pre: a StringSet object
    // Post: returns a new StringSet which is the union 
    //       of this  with otherset, ie. the items in either StringSets
    StringSet StringSet::set_union(StringSet otherset)
    {
     
    }
    
    
    // print
    // Pre: None
    // Post: the stringset will be printed to cout
    void StringSet::print()
    {
         for( int i=0; i<contents.size(); i++ ) 
            cout << "\t" << contents[i] << endl;
    }
    and this is my mainc.pp

    Code:
    #include "inverted_index.h"
    
    int main() 
    {
    
      StringSet ss;
    
      ss.insert("one");
      ss.insert("two");
      ss.insert("three");
      ss.insert("one");
      
    
      ss.print();
      if ( ss.contains("one") ) 
        cout << "ok - contains('one')\n";
      else
        cout << "fail - contains('one')\n";
    /*   
      // test the InvertedIndex 
     InvertedIndex idx;
     idx.tally("cat", "cats.txt");
     idx.tally("cat", "animals.txt");
     idx.tally("cat", "everything.txt");
     idx.tally("dog", "dogs.txt");
     
     idx.print();
      // searching idx for cat should give us three files
      vector <string> query;
      query.push_back("cat");
      StringSet si = idx.search(query);
      if (si.contains("cats.txt") && si.contains("animals.txt") && si.contains("everything.txt") )
        cout << "ok - search('cat')\n";
      else 
        cout << "fail - search('cat')\n";
    
      if (si.contains("dogs.txt"))
        cout << "fail - search('cat') -- dogs.txt found\n";
      else 
        cout << "ok - search('cat') no dogs\n";
    
    */ 
      system("PAUSE");
      return 0;
    }
    this is another implementation which might be helpful ...

    Code:
    #include "inverted_index.h"
    #include "osdir.h"
    
    
    
    // InvertedIndex::tally
    //   Pre: a word and file pair as strings
    //   Post: adds the file to the index under word
     void InvertedIndex::tally(string word, string file) 
    {
          storage.push_back(word);
          //storage.push_back(file);
          
          //vector<string>::iterator theIterator = storage.end();
          //storage.insert( theIterator, file );
    }
    
    // InvertedIndex::search 
    // Pre: a vector of strings as search terms
    // Post: a vector of strings being the files that all of the
    //      input words occur in in the index.
    StringSet InvertedIndex::search(vector <string> words) 
    {
    
    }
    
    // print
    // Pre: an index structure
    // Post: renders the index on the screen
    void InvertedIndex::print() 
    {
         for(int i = 0; i < storage.size(); i++)
         {
             cout<<endl<<storage[i]<<" ";
         }
         cout<<endl<<endl;
    }
    
    //
    // These  methods have been written for you, there is no need to change 
    // anything below this text.  These methods make use of the other
    // methods defined above.
    //
    
    
    // IndexFile
    //  Pre: a file name as a string
    //  Post: the words in the file are added to the inverted index
    void InvertedIndex::indexFile( string filename ) {
    
    	vector <string> words = ReadFile(filename);
    	for(int i=0; i<words.size(); i++) {
    		tally(words[i], filename);
    	}
    }
    
    void InvertedIndex::indexDir( string dirname ) {
    
      string filename; 
      oslink::directory dir(dirname);
      while (dir) {
         filename = dirname + "/" + dir.next();
         indexFile(filename);
      } 
    }

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    The intersection of two sets is the set that contains the elements that are present in both of the two sets. That is, if c is an element in "this" set, then c is in the intersection if and only if otherset.contains(c). So a very basic algorithm could create an empty set to hold the intersection, go through the elements in one of the sets and check if they are in the other one too, and if they are, add them to the intersection set.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    can you provide some sample as i am relly newbie on this ... thank you .. and will really appreciated..

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    i am trying to do it, this is my code.. but please correct for me ..
    (anyone)

    Code:
    StringSet StringSet::set_intersection(StringSet otherset)
    {
        stringset result;
    
        for(int i = 0; i < contents.size(); i++)
            if(otherset.contains(contents[i])
                result = contents[i];  
    }

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    > result = contents[i];

    Instead of setting result to contents[i], you want to insert contents[i] to the result. So something like
    Code:
    result.insert(contents[i]);

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    will this work??

    Code:
    StringSet StringSet::set_intersection(StringSet otherset)
    {
        stringset result;
    
        for(int i = 0; i < contents.size(); i++)
            if(otherset.contains(contents[i])
                result.insert(contents[i]);
    }

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    Have you tried it?

    Is your class called stringset or StringSet? You use both.

    The function is declared to return StringSet but you don't have a "return" statement.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    peter_hii, is this homework? If it is not, then you really might want to use what I suggested in your other thread.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    Quote Originally Posted by joni
    Have you tried it?

    Is your class called stringset or StringSet? You use both.

    The function is declared to return StringSet but you don't have a "return" statement.
    it is StringSet...

    Code:
    StringSet StringSet::set_intersection(StringSet otherset)
    {
        StringSet result;
    
        for(int i = 0; i < contents.size(); i++)
            if(otherset.contains(contents[i])
                result.insert(contents[i]);
    }
    got error

    L:\assignment 2\stringset.cpp In member function `StringSet StringSet::set_intersection(StringSet)':
    56 L:\assignment 2\stringset.cpp expected `)' before "result"
    L:\assignment 2\Makefile.win [Build Error] [stringset.o] Error 1
    Last edited by peter_hii; 09-28-2006 at 03:31 AM.

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    Quote Originally Posted by laserlight
    peter_hii, is this homework? If it is not, then you really might want to use what I suggested in your other thread.
    the format is unchangeble.. but really appreciate that you done that.. really make sense for me ...
    and using set is really easy but i want to stick with my own format......

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    the format is unchangeble.. but really appreciate that you done that.. really make sense for me ...
    and using set is really easy but i want to stick with my own format......
    If that is the case, then use your StringSet class as a wrapper class for std::set<std::string>, instead of making it a typedef.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    Quote Originally Posted by peter_hii
    L:\assignment 2\stringset.cpp In member function `StringSet StringSet::set_intersection(StringSet)':
    56 L:\assignment 2\stringset.cpp expected `)' before "result"
    L:\assignment 2\Makefile.win [Build Error] [stringset.o] Error 1
    Well, like it says, the compiler is expecting to find a ")" before the word "result" on line 56. It doesn't get plainer than that. You are missing a closing parenthesis.

  13. #13
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    Quote Originally Posted by laserlight
    If that is the case, then use your StringSet class as a wrapper class for std::set<std::string>, instead of making it a typedef.
    you mean ??

    for the declaration// make it set<string> stringset; ???

  14. #14
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    Quote Originally Posted by joni
    Well, like it says, the compiler is expecting to find a ")" before the word "result" on line 56. It doesn't get plainer than that. You are missing a closing parenthesis.
    yap as what you said and machine error.... i miss one " )" .. very appreciated... i am trying on the code now ....

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    for the declaration// make it set<string> stringset; ???
    No. Here is an example of a wrapper:
    Code:
    class StringSet
    {
    public:
        // inserts value to contents
        void insert(const std::string& value)
        {
            contents_.insert(value);
        }
    
        // returns true if string set contents contains value
        bool contains(const std::string& value) const
        {
            return contents_.find(value) != contents_.end();
        }
    private:
        std::set<std::string> contents_;
    };
    Of course, you would have your set_intersection(), set_union() and print() member functions implemented in terms of the std::set<std::string> member variable, possibly using the std::set_intersection() and std::set_union generic algorithms that I demonstrated in the other thread.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. project idea ???
    By gemini_shooter in forum C Programming
    Replies: 2
    Last Post: 06-09-2005, 10:56 AM
  2. Have an idea?
    By B0bDole in forum Projects and Job Recruitment
    Replies: 46
    Last Post: 01-13-2005, 03:25 PM
  3. A little problem about an idea, help please
    By louis_mine in forum C++ Programming
    Replies: 3
    Last Post: 09-10-2004, 10:52 PM
  4. totally screwed up idea
    By iain in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 08-17-2001, 01:09 PM

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