Thread: Help with Refining the set_union function in C++

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

    Question Help with Refining the set_union function in C++

    Thanks all for your help. A true lifeline. However, as mentioned I need to implement a set class. I am mocking the STL Library set_union function in c++. Essentially,the intersection is performed on sets, not on strings. The set class ensures that each element is only stored once. Perhaps if this was written first then writing intersection is somewhat easier.
    The set is stored in a vector which has the size() method therefore in the loop i need to replace vectore.length() with vector.size().

    Thanks in advance.

    Code:
    // 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)  //this line cannot change. I only want    to pass in 2 objects.
    
    {
        // record chars that have already been found to intersect so that we don't
        // add them to result more than once
        int hits[256];
        //memset(hits, 0, sizeof(hits));
               
        string result;
        char cur;
        for ( int i = 0; i < contents.size(); i++) {
            cur = contents[i];
            // check if we already found char
            if (contents.find(cur) != string::npos) {
                if (hits[(int)cur] == 0) {
                    // found new char
                    hits[(int)cur] = 1;
                    result += cur;
                } else {
                    // already found char so don't add to result
                }
            }
        }
    
        return result;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What is the question?

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    9

    Unhappy

    The function doesnt work as it stands. Can someone please assist me in fixing the errors inside it.
    Ta, I

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What doesn't work?
    - doesn't compile?
    - produces no answer?
    - produces wrong answers?

    Test it with some really simple sets, like "a" and "", "a" and "a", "a" and "b", "a" and "ab"
    Step through the code with a debugger and observe what happens.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM