Help with finding the intersection of 2 strings

This is a discussion on Help with finding the intersection of 2 strings within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm just about done with implemting code for procurring the intersection of 2 strings and returns a 3rd string ...

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

    Question Help with finding the intersection of 2 strings

    Hi,
    I'm just about done with implemting code for procurring the intersection of 2 strings and returns a 3rd string that is the intersection. However, i have some errors occuring, refer to the code below.
    Any help would be appreciated,
    thanks,
    I

    Code:
    StringSet StringSet::set_intersection(set1 set2)
    {
        // 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]; //ERROR OCCURS HERE cannot convert `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to `char' in assignment 
           
    
            if (contents.find(cur) != string::npos)//ERROR OCCURS HERE, CANNOT USE "FIND FUNCTION" {
                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; //ERROR conversion from `std::string' to non-scalar type `StringSet' requested 
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >cur = contents[i]; //ERROR
    Presumably, contents is an array or vector of strings. You can't assign a string to a char.

    >if (contents.find(cur) != string::npos)//ERROR
    Perhaps you meant:
    Code:
    if (contents[i].find(cur) != string::npos)
    >return result; //ERROR
    result is defined as a string but your function returns a StringSet. If you're getting an error, the two types are not compatible.
    My best code is written with the delete key.

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

    returning a StringSet?

    how do i then go about returning a StringSet?
    ta,
    I

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >how do i then go about returning a StringSet?
    Maybe change result to a StringSet and use the interface you've defined to fill it with whatever your results are? I really have no idea how to use a StringSet since it appears that you're the one defining it, and you haven't shown us the definition.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    9
    Here's my stringset.h header file:
    Code:
    #include <vector>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    class StringSet 
    {
    private: 
    
    vector <string> contents;
     
    public:
    
    void insert(string value);
      
    int contains(string value); 
      
    StringSet set_intersection(StringSet otherset);
      
    StringSet set_union(StringSet otherset);          
      
      
    void print();
      
    };

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Here's my stringset.h header file:
    Um, we're not going to tell you how to use a class that you write yourself. If you wrote it then you should have no problem using it. If you do have a problem, then you have more of a problem than returning a StringSet.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Finding a string's pixel width
    By @nthony in forum Windows Programming
    Replies: 2
    Last Post: 08-03-2006, 02:11 AM
  4. Finding similar words from 2 strings
    By lzhaol in forum C++ Programming
    Replies: 7
    Last Post: 02-26-2006, 12:45 AM
  5. finding strings in strings
    By watshamacalit in forum C Programming
    Replies: 14
    Last Post: 01-11-2003, 12:08 AM

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