Thread: Program flow

  1. #16
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    I rewrote the code but im having trouble defining the InputIterator1 first1, InputIterator1 last1 members when using strings with set_intersection.

    Code:
    void read_file(const char*, set<string>& );
     
    int main ()
    {
       set<string> sWords1;
       set<string> sWords2;
      
       read_file("1.txt", sWords1);
       read_file("2.txt", sWords2);
      
       vector<string> v;
       vector<string>::iterator it;
      
       it = set_intersection (sWords1, sWords1.size(), sWords2, sWords2.size(), v.begin());   // having trouble here
      
       cout << "Intersection has " << int(it - v.begin()) << " elements.\n";
      
       for (size_t i = 0; i != v.size(); ++i)
              cout << "The "<< i+1 <<"." << " element is: " << v[i] << "\n";
      
      system("pause");
      return 0;
    }
      
      void read_file(const char* name, vector<string>& vWords)
      {
        ifstream file(name);
        string line;
        vWords.clear();
        while (getline(file, line, ' ')) {
            vWords.push_back(line);
        }
      }
    Last edited by Ducky; 01-09-2008 at 06:29 AM.

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You don't have space in your vector for the intersection. Use a std::back_inserter(v) instead of v.begin(). Then you can just use v.size() to get the number of elements.

    Use sWords1.begin(), sWords1.end(), sWords2.begin(), sWords2.end() as the other arguments.


    Also, read_file takes a vector, but you're trying to pass a set. That won't work. Either make sWords1/2 vectors (but then you need to sort them afterwards), or make read_file take a set and change the insertion function accordingly.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thank you very much!
    Last edited by Ducky; 01-09-2008 at 08:47 AM.

  4. #19
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    I chose to make sWords1/2 vectors because the set container doesnt have a push_back function.
    I sorted vWords1/2 out but i still get an error.

    Code:
    int main ()
    {
      vector<string> vWords1;
      vector<string> vWords2;
      
      read_file("1.txt", vWords1);
      read_file("2.txt", vWords2);
    
      sort (vWords1.begin(), vWords1.end()); 
      sort (vWords2.begin(), vWords2.end());  
      
      vector<string> v;
      vector<string>::iterator it;
      
      it = set_intersection (vWords1.begin(), vWords1.end(), vWords2.begin(), vWords2.end(), back_inserter(v));
      // error here
      cout << "Intersection has " << v.size() << " elements.\n";
      for (size_t i = 0; i != v.size(); ++i)
        cout << "The "<< i+1 <<"." << " element is: " << v[i] << "\n";
      
      system("pause");
      return 0;
    }
      void read_file(const char* name, vector<string>& vWords)
      {
        ifstream file(name);
        string line;
        vWords.clear();
        while (getline(file, line)) {
            vWords.push_back(line);
        }
       }
    Last edited by Ducky; 01-09-2008 at 09:18 AM.

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    what error?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #21
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    no match for 'operator=' in 'it = std::set_intersection [with_InputIterator1=__gnu_cxx::__normal_iterator<s td::string* ... etc.

    btw i included #include <iterator>
    Last edited by Ducky; 01-09-2008 at 09:38 AM.

  7. #22
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Oh yeah. Don't assign the return value to anything. It's useless in this case. You just get back the back_insert_iterator you passed in. Just use the vector as it is. If you need a pair of iterators for the intersection, use v.begin() and v.end().
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #23
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Cool, it works! Thank you so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM