Thread: search in vector .. anyidea.??

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

    search in vector .. anyidea.??

    hi everyone ... first of all, i let you guyz know what i am doing here... and will list the problem tat i got....

    well i want to performs a search for the seqence of words finding files containing all of the search words...

    in my implememntation (.cpp)

    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) 
    {
         inputType input(word, file);
         list.push_back( input );
    }
    
    // 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) 
    {
         for(int i = 0; i < list.size(); i++)
         {
               if(list.word[i] == words)
                  return list.file[i];
         }        
    }
    and the main is

    Code:
    #include "inverted_index.h"
    
    int main() 
    {
      // 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");
    
       // 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;
    }
    can someone check my code for the red font.... please//

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Code:
    system("pause")
    Please don't use this, use

    Code:
    cin.get();
    Only use a system command if you really have to, but there is always another way around using one.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    Quote Originally Posted by swgh
    Code:
    system("pause")
    Please don't use this, use

    Code:
    cin.get();
    Only use a system command if you really have to, but there is always another way around using one.

    why is that ? please explain... thank you...

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    system pause is known to be a bad choice for feezing the console window. Read the FAQ on this site for a detailed explaination. I will not go into an argument about why it should / should not be used but in general program practice, system pause is frowned upon. Cin.get() is a cleaner and more efficient way to pause the console window.

    As I stated above, read the FAQ for a good reason to not use system pause. I belive it has somthing to do with program security issues.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    can someone give me an idea of how to design this function please?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What's an inputType? What is the type of the list member variable? What is a StringSet? An Inverted Index?

    Really, post the smallest and simplest compilable program that demonstrates the error/problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

    Smile

    you have PM laserlight.....

  8. #8
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Just post here, it makes me, I mean me, feel left out.

    Code:
               if(list.word[i] == words)
                  return list.file[i];
    Unless list.words[i] has an operator vector<string>() and an operator StringSet, that section of code will not work. In fact, that whole thing is not logical. Edited.
    Last edited by Tonto; 10-02-2006 at 06:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. 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
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 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